1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package javax.swing;
27
28 import java.util.*;
29
30 import java.applet.Applet;
31 import java.awt.*;
32 import java.awt.event.*;
33 import java.awt.print.*;
34
35 import java.beans.*;
36
37 import java.io.Serializable;
38 import java.io.ObjectOutputStream;
39 import java.io.ObjectInputStream;
40 import java.io.IOException;
41
42 import javax.accessibility.*;
43
44 import javax.swing.event.*;
45 import javax.swing.plaf.*;
46 import javax.swing.table.*;
47 import javax.swing.border.*;
48
49 import java.text.NumberFormat;
50 import java.text.DateFormat;
51 import java.text.MessageFormat;
52
53 import javax.print.attribute.*;
54 import javax.print.PrintService;
55
56 import sun.swing.SwingUtilities2;
57 import sun.swing.SwingUtilities2.Section;
58 import static sun.swing.SwingUtilities2.Section.*;
59 import sun.swing.PrintingStatus;
60 import sun.swing.SwingLazyValue;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 public class JTable extends JComponent implements TableModelListener, Scrollable,
221 TableColumnModelListener, ListSelectionListener, CellEditorListener,
222 Accessible, RowSorterListener
223 {
224
225
226
227
228
229
230
231
232 private static final String uiClassID = "TableUI";
233
234
235 public static final int AUTO_RESIZE_OFF = 0;
236
237
238 public static final int AUTO_RESIZE_NEXT_COLUMN = 1;
239
240
241
242 public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS = 2;
243
244
245 public static final int AUTO_RESIZE_LAST_COLUMN = 3;
246
247
248 public static final int AUTO_RESIZE_ALL_COLUMNS = 4;
249
250
251
252
253
254
255
256
257
258
259 public enum PrintMode {
260
261
262
263
264
265 NORMAL,
266
267
268
269
270
271
272 FIT_WIDTH
273 }
274
275
276
277
278
279
280
281 protected TableModel dataModel;
282
283
284 protected TableColumnModel columnModel;
285
286
287 protected ListSelectionModel selectionModel;
288
289
290 protected JTableHeader tableHeader;
291
292
293 protected int rowHeight;
294
295
296 protected int rowMargin;
297
298
299 protected Color gridColor;
300
301
302 protected boolean showHorizontalLines;
303
304
305 protected boolean showVerticalLines;
306
307
308
309
310
311
312 protected int autoResizeMode;
313
314
315
316
317
318 protected boolean autoCreateColumnsFromModel;
319
320
321 protected Dimension preferredViewportSize;
322
323
324 protected boolean rowSelectionAllowed;
325
326
327
328
329
330
331
332
333
334
335
336
337
338 protected boolean cellSelectionEnabled;
339
340
341 transient protected Component editorComp;
342
343
344
345
346
347
348 transient protected TableCellEditor cellEditor;
349
350
351 transient protected int editingColumn;
352
353
354 transient protected int editingRow;
355
356
357
358
359
360
361 transient protected Hashtable defaultRenderersByColumnClass;
362
363
364
365
366
367
368 transient protected Hashtable defaultEditorsByColumnClass;
369
370
371 protected Color selectionForeground;
372
373
374 protected Color selectionBackground;
375
376
377
378
379
380
381
382 private SizeSequence rowModel;
383 private boolean dragEnabled;
384 private boolean surrendersFocusOnKeystroke;
385 private PropertyChangeListener editorRemover = null;
386
387
388
389
390
391 private boolean columnSelectionAdjusting;
392
393
394
395
396 private boolean rowSelectionAdjusting;
397
398
399
400
401 private Throwable printError;
402
403
404
405
406 private boolean isRowHeightSet;
407
408
409
410
411 private boolean updateSelectionOnSort;
412
413
414
415
416 private transient SortManager sortManager;
417
418
419
420
421 private boolean ignoreSortChange;
422
423
424
425
426 private boolean sorterChanged;
427
428
429
430
431 private boolean autoCreateRowSorter;
432
433
434
435
436
437
438 private boolean fillsViewportHeight;
439
440
441
442
443 private DropMode dropMode = DropMode.USE_SELECTION;
444
445
446
447
448 private transient DropLocation dropLocation;
449
450
451
452
453
454
455
456
457 public static final class DropLocation extends TransferHandler.DropLocation {
458 private final int row;
459 private final int col;
460 private final boolean isInsertRow;
461 private final boolean isInsertCol;
462
463 private DropLocation(Point p, int row, int col,
464 boolean isInsertRow, boolean isInsertCol) {
465
466 super(p);
467 this.row = row;
468 this.col = col;
469 this.isInsertRow = isInsertRow;
470 this.isInsertCol = isInsertCol;
471 }
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487 public int getRow() {
488 return row;
489 }
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505 public int getColumn() {
506 return col;
507 }
508
509
510
511
512
513
514
515 public boolean isInsertRow() {
516 return isInsertRow;
517 }
518
519
520
521
522
523
524
525 public boolean isInsertColumn() {
526 return isInsertCol;
527 }
528
529
530
531
532
533
534
535
536
537 public String toString() {
538 return getClass().getName()
539 + "[dropPoint=" + getDropPoint() + ","
540 + "row=" + row + ","
541 + "column=" + col + ","
542 + "insertRow=" + isInsertRow + ","
543 + "insertColumn=" + isInsertCol + "]";
544 }
545 }
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560 public JTable() {
561 this(null, null, null);
562 }
563
564
565
566
567
568
569
570
571
572
573 public JTable(TableModel dm) {
574 this(dm, null, null);
575 }
576
577
578
579
580
581
582
583
584
585
586 public JTable(TableModel dm, TableColumnModel cm) {
587 this(dm, cm, null);
588 }
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608 public JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
609 super();
610 setLayout(null);
611
612 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
613 JComponent.getManagingFocusForwardTraversalKeys());
614 setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
615 JComponent.getManagingFocusBackwardTraversalKeys());
616 if (cm == null) {
617 cm = createDefaultColumnModel();
618 autoCreateColumnsFromModel = true;
619 }
620 setColumnModel(cm);
621
622 if (sm == null) {
623 sm = createDefaultSelectionModel();
624 }
625 setSelectionModel(sm);
626
627
628
629
630 if (dm == null) {
631 dm = createDefaultDataModel();
632 }
633 setModel(dm);
634
635 initializeLocalVars();
636 updateUI();
637 }
638
639
640
641
642
643
644
645
646
647
648
649 public JTable(int numRows, int numColumns) {
650 this(new DefaultTableModel(numRows, numColumns));
651 }
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667 public JTable(Vector rowData, Vector columnNames) {
668 this(new DefaultTableModel(rowData, columnNames));
669 }
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684 public JTable(final Object[][] rowData, final Object[] columnNames) {
685 this(new AbstractTableModel() {
686 public String getColumnName(int column) { return columnNames[column].toString(); }
687 public int getRowCount() { return rowData.length; }
688 public int getColumnCount() { return columnNames.length; }
689 public Object getValueAt(int row, int col) { return rowData[row][col]; }
690 public boolean isCellEditable(int row, int column) { return true; }
691 public void setValueAt(Object value, int row, int col) {
692 rowData[row][col] = value;
693 fireTableCellUpdated(row, col);
694 }
695 });
696 }
697
698
699
700
701
702
703 public void addNotify() {
704 super.addNotify();
705 configureEnclosingScrollPane();
706 }
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721 protected void configureEnclosingScrollPane() {
722 Container parent = SwingUtilities.getUnwrappedParent(this);
723 if (parent instanceof JViewport) {
724 JViewport port = (JViewport) parent;
725 Container gp = port.getParent();
726 if (gp instanceof JScrollPane) {
727 JScrollPane scrollPane = (JScrollPane)gp;
728
729
730
731 JViewport viewport = scrollPane.getViewport();
732 if (viewport == null ||
733 SwingUtilities.getUnwrappedView(viewport) != this) {
734 return;
735 }
736 scrollPane.setColumnHeaderView(getTableHeader());
737
738 configureEnclosingScrollPaneUI();
739 }
740 }
741 }
742
743
744
745
746
747
748
749
750
751
752
753
754
755 private void configureEnclosingScrollPaneUI() {
756 Container parent = SwingUtilities.getUnwrappedParent(this);
757 if (parent instanceof JViewport) {
758 JViewport port = (JViewport) parent;
759 Container gp = port.getParent();
760 if (gp instanceof JScrollPane) {
761 JScrollPane scrollPane = (JScrollPane)gp;
762
763
764
765 JViewport viewport = scrollPane.getViewport();
766 if (viewport == null ||
767 SwingUtilities.getUnwrappedView(viewport) != this) {
768 return;
769 }
770
771 Border border = scrollPane.getBorder();
772 if (border == null || border instanceof UIResource) {
773 Border scrollPaneBorder =
774 UIManager.getBorder("Table.scrollPaneBorder");
775 if (scrollPaneBorder != null) {
776 scrollPane.setBorder(scrollPaneBorder);
777 }
778 }
779
780 Component corner =
781 scrollPane.getCorner(JScrollPane.UPPER_TRAILING_CORNER);
782 if (corner == null || corner instanceof UIResource){
783 corner = null;
784 Object componentClass = UIManager.get(
785 "Table.scrollPaneCornerComponent");
786 if (componentClass instanceof Class){
787 try {
788 corner = (Component)
789 ((Class)componentClass).newInstance();
790 } catch (Exception e) {
791
792 }
793 }
794 scrollPane.setCorner(JScrollPane.UPPER_TRAILING_CORNER,
795 corner);
796 }
797 }
798 }
799 }
800
801
802
803
804
805
806 public void removeNotify() {
807 KeyboardFocusManager.getCurrentKeyboardFocusManager().
808 removePropertyChangeListener("permanentFocusOwner", editorRemover);
809 editorRemover = null;
810 unconfigureEnclosingScrollPane();
811 super.removeNotify();
812 }
813
814
815
816
817
818
819
820
821
822
823
824
825
826 protected void unconfigureEnclosingScrollPane() {
827 Container parent = SwingUtilities.getUnwrappedParent(this);
828 if (parent instanceof JViewport) {
829 JViewport port = (JViewport) parent;
830 Container gp = port.getParent();
831 if (gp instanceof JScrollPane) {
832 JScrollPane scrollPane = (JScrollPane)gp;
833
834
835
836 JViewport viewport = scrollPane.getViewport();
837 if (viewport == null ||
838 SwingUtilities.getUnwrappedView(viewport) != this) {
839 return;
840 }
841 scrollPane.setColumnHeaderView(null);
842
843 Component corner =
844 scrollPane.getCorner(JScrollPane.UPPER_TRAILING_CORNER);
845 if (corner instanceof UIResource){
846 scrollPane.setCorner(JScrollPane.UPPER_TRAILING_CORNER,
847 null);
848 }
849 }
850 }
851 }
852
853 void setUIProperty(String propertyName, Object value) {
854 if (propertyName == "rowHeight") {
855 if (!isRowHeightSet) {
856 setRowHeight(((Number)value).intValue());
857 isRowHeightSet = false;
858 }
859 return;
860 }
861 super.setUIProperty(propertyName, value);
862 }
863
864
865
866
867
868
869
870
871
872
873
874 @Deprecated
875 static public JScrollPane createScrollPaneForTable(JTable aTable) {
876 return new JScrollPane(aTable);
877 }
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893 public void setTableHeader(JTableHeader tableHeader) {
894 if (this.tableHeader != tableHeader) {
895 JTableHeader old = this.tableHeader;
896
897 if (old != null) {
898 old.setTable(null);
899 }
900 this.tableHeader = tableHeader;
901 if (tableHeader != null) {
902 tableHeader.setTable(this);
903 }
904 firePropertyChange("tableHeader", old, tableHeader);
905 }
906 }
907
908
909
910
911
912
913
914 public JTableHeader getTableHeader() {
915 return tableHeader;
916 }
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932 public void setRowHeight(int rowHeight) {
933 if (rowHeight <= 0) {
934 throw new IllegalArgumentException("New row height less than 1");
935 }
936 int old = this.rowHeight;
937 this.rowHeight = rowHeight;
938 rowModel = null;
939 if (sortManager != null) {
940 sortManager.modelRowSizes = null;
941 }
942 isRowHeightSet = true;
943 resizeAndRepaint();
944 firePropertyChange("rowHeight", old, rowHeight);
945 }
946
947
948
949
950
951
952
953 public int getRowHeight() {
954 return rowHeight;
955 }
956
957 private SizeSequence getRowModel() {
958 if (rowModel == null) {
959 rowModel = new SizeSequence(getRowCount(), getRowHeight());
960 }
961 return rowModel;
962 }
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979 public void setRowHeight(int row, int rowHeight) {
980 if (rowHeight <= 0) {
981 throw new IllegalArgumentException("New row height less than 1");
982 }
983 getRowModel().setSize(row, rowHeight);
984 if (sortManager != null) {
985 sortManager.setViewRowHeight(row, rowHeight);
986 }
987 resizeAndRepaint();
988 }
989
990
991
992
993
994
995
996 public int getRowHeight(int row) {
997 return (rowModel == null) ? getRowHeight() : rowModel.getSize(row);
998 }
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009 public void setRowMargin(int rowMargin) {
1010 int old = this.rowMargin;
1011 this.rowMargin = rowMargin;
1012 resizeAndRepaint();
1013 firePropertyChange("rowMargin", old, rowMargin);
1014 }
1015
1016
1017
1018
1019
1020
1021
1022
1023 public int getRowMargin() {
1024 return rowMargin;
1025 }
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040 public void setIntercellSpacing(Dimension intercellSpacing) {
1041
1042 setRowMargin(intercellSpacing.height);
1043 getColumnModel().setColumnMargin(intercellSpacing.width);
1044
1045 resizeAndRepaint();
1046 }
1047
1048
1049
1050
1051
1052
1053
1054
1055 public Dimension getIntercellSpacing() {
1056 return new Dimension(getColumnModel().getColumnMargin(), rowMargin);
1057 }
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070 public void setGridColor(Color gridColor) {
1071 if (gridColor == null) {
1072 throw new IllegalArgumentException("New color is null");
1073 }
1074 Color old = this.gridColor;
1075 this.gridColor = gridColor;
1076 firePropertyChange("gridColor", old, gridColor);
1077
1078 repaint();
1079 }
1080
1081
1082
1083
1084
1085
1086
1087
1088 public Color getGridColor() {
1089 return gridColor;
1090 }
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106 public void setShowGrid(boolean showGrid) {
1107 setShowHorizontalLines(showGrid);
1108 setShowVerticalLines(showGrid);
1109
1110
1111 repaint();
1112 }
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126 public void setShowHorizontalLines(boolean showHorizontalLines) {
1127 boolean old = this.showHorizontalLines;
1128 this.showHorizontalLines = showHorizontalLines;
1129 firePropertyChange("showHorizontalLines", old, showHorizontalLines);
1130
1131
1132 repaint();
1133 }
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147 public void setShowVerticalLines(boolean showVerticalLines) {
1148 boolean old = this.showVerticalLines;
1149 this.showVerticalLines = showVerticalLines;
1150 firePropertyChange("showVerticalLines", old, showVerticalLines);
1151
1152 repaint();
1153 }
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163 public boolean getShowHorizontalLines() {
1164 return showHorizontalLines;
1165 }
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175 public boolean getShowVerticalLines() {
1176 return showVerticalLines;
1177 }
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202 public void setAutoResizeMode(int mode) {
1203 if ((mode == AUTO_RESIZE_OFF) ||
1204 (mode == AUTO_RESIZE_NEXT_COLUMN) ||
1205 (mode == AUTO_RESIZE_SUBSEQUENT_COLUMNS) ||
1206 (mode == AUTO_RESIZE_LAST_COLUMN) ||
1207 (mode == AUTO_RESIZE_ALL_COLUMNS)) {
1208 int old = autoResizeMode;
1209 autoResizeMode = mode;
1210 resizeAndRepaint();
1211 if (tableHeader != null) {
1212 tableHeader.resizeAndRepaint();
1213 }
1214 firePropertyChange("autoResizeMode", old, autoResizeMode);
1215 }
1216 }
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227 public int getAutoResizeMode() {
1228 return autoResizeMode;
1229 }
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243 public void setAutoCreateColumnsFromModel(boolean autoCreateColumnsFromModel) {
1244 if (this.autoCreateColumnsFromModel != autoCreateColumnsFromModel) {
1245 boolean old = this.autoCreateColumnsFromModel;
1246 this.autoCreateColumnsFromModel = autoCreateColumnsFromModel;
1247 if (autoCreateColumnsFromModel) {
1248 createDefaultColumnsFromModel();
1249 }
1250 firePropertyChange("autoCreateColumnsFromModel", old, autoCreateColumnsFromModel);
1251 }
1252 }
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266 public boolean getAutoCreateColumnsFromModel() {
1267 return autoCreateColumnsFromModel;
1268 }
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280 public void createDefaultColumnsFromModel() {
1281 TableModel m = getModel();
1282 if (m != null) {
1283
1284 TableColumnModel cm = getColumnModel();
1285 while (cm.getColumnCount() > 0) {
1286 cm.removeColumn(cm.getColumn(0));
1287 }
1288
1289
1290 for (int i = 0; i < m.getColumnCount(); i++) {
1291 TableColumn newColumn = new TableColumn(i);
1292 addColumn(newColumn);
1293 }
1294 }
1295 }
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308 public void setDefaultRenderer(Class<?> columnClass, TableCellRenderer renderer) {
1309 if (renderer != null) {
1310 defaultRenderersByColumnClass.put(columnClass, renderer);
1311 }
1312 else {
1313 defaultRenderersByColumnClass.remove(columnClass);
1314 }
1315 }
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332 public TableCellRenderer getDefaultRenderer(Class<?> columnClass) {
1333 if (columnClass == null) {
1334 return null;
1335 }
1336 else {
1337 Object renderer = defaultRenderersByColumnClass.get(columnClass);
1338 if (renderer != null) {
1339 return (TableCellRenderer)renderer;
1340 }
1341 else {
1342 Class c = columnClass.getSuperclass();
1343 if (c == null && columnClass != Object.class) {
1344 c = Object.class;
1345 }
1346 return getDefaultRenderer(c);
1347 }
1348 }
1349 }
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366 public void setDefaultEditor(Class<?> columnClass, TableCellEditor editor) {
1367 if (editor != null) {
1368 defaultEditorsByColumnClass.put(columnClass, editor);
1369 }
1370 else {
1371 defaultEditorsByColumnClass.remove(columnClass);
1372 }
1373 }
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389 public TableCellEditor getDefaultEditor(Class<?> columnClass) {
1390 if (columnClass == null) {
1391 return null;
1392 }
1393 else {
1394 Object editor = defaultEditorsByColumnClass.get(columnClass);
1395 if (editor != null) {
1396 return (TableCellEditor)editor;
1397 }
1398 else {
1399 return getDefaultEditor(columnClass.getSuperclass());
1400 }
1401 }
1402 }
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438 public void setDragEnabled(boolean b) {
1439 if (b && GraphicsEnvironment.isHeadless()) {
1440 throw new HeadlessException();
1441 }
1442 dragEnabled = b;
1443 }
1444
1445
1446
1447
1448
1449
1450
1451
1452 public boolean getDragEnabled() {
1453 return dragEnabled;
1454 }
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488 public final void setDropMode(DropMode dropMode) {
1489 if (dropMode != null) {
1490 switch (dropMode) {
1491 case USE_SELECTION:
1492 case ON:
1493 case INSERT:
1494 case INSERT_ROWS:
1495 case INSERT_COLS:
1496 case ON_OR_INSERT:
1497 case ON_OR_INSERT_ROWS:
1498 case ON_OR_INSERT_COLS:
1499 this.dropMode = dropMode;
1500 return;
1501 }
1502 }
1503
1504 throw new IllegalArgumentException(dropMode + ": Unsupported drop mode for table");
1505 }
1506
1507
1508
1509
1510
1511
1512
1513
1514 public final DropMode getDropMode() {
1515 return dropMode;
1516 }
1517
1518
1519
1520
1521
1522
1523
1524
1525 DropLocation dropLocationForPoint(Point p) {
1526 DropLocation location = null;
1527
1528 int row = rowAtPoint(p);
1529 int col = columnAtPoint(p);
1530 boolean outside = Boolean.TRUE == getClientProperty("Table.isFileList")
1531 && SwingUtilities2.pointOutsidePrefSize(this, row, col, p);
1532
1533 Rectangle rect = getCellRect(row, col, true);
1534 Section xSection, ySection;
1535 boolean between = false;
1536 boolean ltr = getComponentOrientation().isLeftToRight();
1537
1538 switch(dropMode) {
1539 case USE_SELECTION:
1540 case ON:
1541 if (row == -1 || col == -1 || outside) {
1542 location = new DropLocation(p, -1, -1, false, false);
1543 } else {
1544 location = new DropLocation(p, row, col, false, false);
1545 }
1546 break;
1547 case INSERT:
1548 if (row == -1 && col == -1) {
1549 location = new DropLocation(p, 0, 0, true, true);
1550 break;
1551 }
1552
1553 xSection = SwingUtilities2.liesInHorizontal(rect, p, ltr, true);
1554
1555 if (row == -1) {
1556 if (xSection == LEADING) {
1557 location = new DropLocation(p, getRowCount(), col, true, true);
1558 } else if (xSection == TRAILING) {
1559 location = new DropLocation(p, getRowCount(), col + 1, true, true);
1560 } else {
1561 location = new DropLocation(p, getRowCount(), col, true, false);
1562 }
1563 } else if (xSection == LEADING || xSection == TRAILING) {
1564 ySection = SwingUtilities2.liesInVertical(rect, p, true);
1565 if (ySection == LEADING) {
1566 between = true;
1567 } else if (ySection == TRAILING) {
1568 row++;
1569 between = true;
1570 }
1571
1572 location = new DropLocation(p, row,
1573 xSection == TRAILING ? col + 1 : col,
1574 between, true);
1575 } else {
1576 if (SwingUtilities2.liesInVertical(rect, p, false) == TRAILING) {
1577 row++;
1578 }
1579
1580 location = new DropLocation(p, row, col, true, false);
1581 }
1582
1583 break;
1584 case INSERT_ROWS:
1585 if (row == -1 && col == -1) {
1586 location = new DropLocation(p, -1, -1, false, false);
1587 break;
1588 }
1589
1590 if (row == -1) {
1591 location = new DropLocation(p, getRowCount(), col, true, false);
1592 break;
1593 }
1594
1595 if (SwingUtilities2.liesInVertical(rect, p, false) == TRAILING) {
1596 row++;
1597 }
1598
1599 location = new DropLocation(p, row, col, true, false);
1600 break;
1601 case ON_OR_INSERT_ROWS:
1602 if (row == -1 && col == -1) {
1603 location = new DropLocation(p, -1, -1, false, false);
1604 break;
1605 }
1606
1607 if (row == -1) {
1608 location = new DropLocation(p, getRowCount(), col, true, false);
1609 break;
1610 }
1611
1612 ySection = SwingUtilities2.liesInVertical(rect, p, true);
1613 if (ySection == LEADING) {
1614 between = true;
1615 } else if (ySection == TRAILING) {
1616 row++;
1617 between = true;
1618 }
1619
1620 location = new DropLocation(p, row, col, between, false);
1621 break;
1622 case INSERT_COLS:
1623 if (row == -1) {
1624 location = new DropLocation(p, -1, -1, false, false);
1625 break;
1626 }
1627
1628 if (col == -1) {
1629 location = new DropLocation(p, getColumnCount(), col, false, true);
1630 break;
1631 }
1632
1633 if (SwingUtilities2.liesInHorizontal(rect, p, ltr, false) == TRAILING) {
1634 col++;
1635 }
1636
1637 location = new DropLocation(p, row, col, false, true);
1638 break;
1639 case ON_OR_INSERT_COLS:
1640 if (row == -1) {
1641 location = new DropLocation(p, -1, -1, false, false);
1642 break;
1643 }
1644
1645 if (col == -1) {
1646 location = new DropLocation(p, row, getColumnCount(), false, true);
1647 break;
1648 }
1649
1650 xSection = SwingUtilities2.liesInHorizontal(rect, p, ltr, true);
1651 if (xSection == LEADING) {
1652 between = true;
1653 } else if (xSection == TRAILING) {
1654 col++;
1655 between = true;
1656 }
1657
1658 location = new DropLocation(p, row, col, false, between);
1659 break;
1660 case ON_OR_INSERT:
1661 if (row == -1 && col == -1) {
1662 location = new DropLocation(p, 0, 0, true, true);
1663 break;
1664 }
1665
1666 xSection = SwingUtilities2.liesInHorizontal(rect, p, ltr, true);
1667
1668 if (row == -1) {
1669 if (xSection == LEADING) {
1670 location = new DropLocation(p, getRowCount(), col, true, true);
1671 } else if (xSection == TRAILING) {
1672 location = new DropLocation(p, getRowCount(), col + 1, true, true);
1673 } else {
1674 location = new DropLocation(p, getRowCount(), col, true, false);
1675 }
1676
1677 break;
1678 }
1679
1680 ySection = SwingUtilities2.liesInVertical(rect, p, true);
1681 if (ySection == LEADING) {
1682 between = true;
1683 } else if (ySection == TRAILING) {
1684 row++;
1685 between = true;
1686 }
1687
1688 location = new DropLocation(p, row,
1689 xSection == TRAILING ? col + 1 : col,
1690 between,
1691 xSection != MIDDLE);
1692
1693 break;
1694 default:
1695 assert false : "Unexpected drop mode";
1696 }
1697
1698 return location;
1699 }
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734 Object setDropLocation(TransferHandler.DropLocation location,
1735 Object state,
1736 boolean forDrop) {
1737
1738 Object retVal = null;
1739 DropLocation tableLocation = (DropLocation)location;
1740
1741 if (dropMode == DropMode.USE_SELECTION) {
1742 if (tableLocation == null) {
1743 if (!forDrop && state != null) {
1744 clearSelection();
1745
1746 int[] rows = ((int[][])state)[0];
1747 int[] cols = ((int[][])state)[1];
1748 int[] anchleads = ((int[][])state)[2];
1749
1750 for (int row : rows) {
1751 addRowSelectionInterval(row, row);
1752 }
1753
1754 for (int col : cols) {
1755 addColumnSelectionInterval(col, col);
1756 }
1757
1758 SwingUtilities2.setLeadAnchorWithoutSelection(
1759 getSelectionModel(), anchleads[1], anchleads[0]);
1760
1761 SwingUtilities2.setLeadAnchorWithoutSelection(
1762 getColumnModel().getSelectionModel(),
1763 anchleads[3], anchleads[2]);
1764 }
1765 } else {
1766 if (dropLocation == null) {
1767 retVal = new int[][]{
1768 getSelectedRows(),
1769 getSelectedColumns(),
1770 {getAdjustedIndex(getSelectionModel()
1771 .getAnchorSelectionIndex(), true),
1772 getAdjustedIndex(getSelectionModel()
1773 .getLeadSelectionIndex(), true),
1774 getAdjustedIndex(getColumnModel().getSelectionModel()
1775 .getAnchorSelectionIndex(), false),
1776 getAdjustedIndex(getColumnModel().getSelectionModel()
1777 .getLeadSelectionIndex(), false)}};
1778 } else {
1779 retVal = state;
1780 }
1781
1782 if (tableLocation.getRow() == -1) {
1783 clearSelectionAndLeadAnchor();
1784 } else {
1785 setRowSelectionInterval(tableLocation.getRow(),
1786 tableLocation.getRow());
1787 setColumnSelectionInterval(tableLocation.getColumn(),
1788 tableLocation.getColumn());
1789 }
1790 }
1791 }
1792
1793 DropLocation old = dropLocation;
1794 dropLocation = tableLocation;
1795 firePropertyChange("dropLocation", old, dropLocation);
1796
1797 return retVal;
1798 }
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818 public final DropLocation getDropLocation() {
1819 return dropLocation;
1820 }
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841 public void setAutoCreateRowSorter(boolean autoCreateRowSorter) {
1842 boolean oldValue = this.autoCreateRowSorter;
1843 this.autoCreateRowSorter = autoCreateRowSorter;
1844 if (autoCreateRowSorter) {
1845 setRowSorter(new TableRowSorter<TableModel>(getModel()));
1846 }
1847 firePropertyChange("autoCreateRowSorter", oldValue,
1848 autoCreateRowSorter);
1849 }
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860 public boolean getAutoCreateRowSorter() {
1861 return autoCreateRowSorter;
1862 }
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877 public void setUpdateSelectionOnSort(boolean update) {
1878 if (updateSelectionOnSort != update) {
1879 updateSelectionOnSort = update;
1880 firePropertyChange("updateSelectionOnSort", !update, update);
1881 }
1882 }
1883
1884
1885
1886
1887
1888
1889
1890 public boolean getUpdateSelectionOnSort() {
1891 return updateSelectionOnSort;
1892 }
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916 public void setRowSorter(RowSorter<? extends TableModel> sorter) {
1917 RowSorter<? extends TableModel> oldRowSorter = null;
1918 if (sortManager != null) {
1919 oldRowSorter = sortManager.sorter;
1920 sortManager.dispose();
1921 sortManager = null;
1922 }
1923 rowModel = null;
1924 clearSelectionAndLeadAnchor();
1925 if (sorter != null) {
1926 sortManager = new SortManager(sorter);
1927 }
1928 resizeAndRepaint();
1929 firePropertyChange("rowSorter", oldRowSorter, sorter);
1930 firePropertyChange("sorter", oldRowSorter, sorter);
1931 }
1932
1933
1934
1935
1936
1937
1938
1939 public RowSorter<? extends TableModel> getRowSorter() {
1940 return (sortManager != null) ? sortManager.sorter : null;
1941 }
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972 public void setSelectionMode(int selectionMode) {
1973 clearSelection();
1974 getSelectionModel().setSelectionMode(selectionMode);
1975 getColumnModel().getSelectionModel().setSelectionMode(selectionMode);
1976 }
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988 public void setRowSelectionAllowed(boolean rowSelectionAllowed) {
1989 boolean old = this.rowSelectionAllowed;
1990 this.rowSelectionAllowed = rowSelectionAllowed;
1991 if (old != rowSelectionAllowed) {
1992 repaint();
1993 }
1994 firePropertyChange("rowSelectionAllowed", old, rowSelectionAllowed);
1995 }
1996
1997
1998
1999
2000
2001
2002
2003 public boolean getRowSelectionAllowed() {
2004 return rowSelectionAllowed;
2005 }
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017 public void setColumnSelectionAllowed(boolean columnSelectionAllowed) {
2018 boolean old = columnModel.getColumnSelectionAllowed();
2019 columnModel.setColumnSelectionAllowed(columnSelectionAllowed);
2020 if (old != columnSelectionAllowed) {
2021 repaint();
2022 }
2023 firePropertyChange("columnSelectionAllowed", old, columnSelectionAllowed);
2024 }
2025
2026
2027
2028
2029
2030
2031
2032 public boolean getColumnSelectionAllowed() {
2033 return columnModel.getColumnSelectionAllowed();
2034 }
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056 public void setCellSelectionEnabled(boolean cellSelectionEnabled) {
2057 setRowSelectionAllowed(cellSelectionEnabled);
2058 setColumnSelectionAllowed(cellSelectionEnabled);
2059 boolean old = this.cellSelectionEnabled;
2060 this.cellSelectionEnabled = cellSelectionEnabled;
2061 firePropertyChange("cellSelectionEnabled", old, cellSelectionEnabled);
2062 }
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073 public boolean getCellSelectionEnabled() {
2074 return getRowSelectionAllowed() && getColumnSelectionAllowed();
2075 }
2076
2077
2078
2079
2080 public void selectAll() {
2081
2082 if (isEditing()) {
2083 removeEditor();
2084 }
2085 if (getRowCount() > 0 && getColumnCount() > 0) {
2086 int oldLead;
2087 int oldAnchor;
2088 ListSelectionModel selModel;
2089
2090 selModel = selectionModel;
2091 selModel.setValueIsAdjusting(true);
2092 oldLead = getAdjustedIndex(selModel.getLeadSelectionIndex(), true);
2093 oldAnchor = getAdjustedIndex(selModel.getAnchorSelectionIndex(), true);
2094
2095 setRowSelectionInterval(0, getRowCount()-1);
2096
2097
2098 SwingUtilities2.setLeadAnchorWithoutSelection(selModel, oldLead, oldAnchor);
2099
2100 selModel.setValueIsAdjusting(false);
2101
2102 selModel = columnModel.getSelectionModel();
2103 selModel.setValueIsAdjusting(true);
2104 oldLead = getAdjustedIndex(selModel.getLeadSelectionIndex(), false);
2105 oldAnchor = getAdjustedIndex(selModel.getAnchorSelectionIndex(), false);
2106
2107 setColumnSelectionInterval(0, getColumnCount()-1);
2108
2109
2110 SwingUtilities2.setLeadAnchorWithoutSelection(selModel, oldLead, oldAnchor);
2111
2112 selModel.setValueIsAdjusting(false);
2113 }
2114 }
2115
2116
2117
2118
2119 public void clearSelection() {
2120 selectionModel.clearSelection();
2121 columnModel.getSelectionModel().clearSelection();
2122 }
2123
2124 private void clearSelectionAndLeadAnchor() {
2125 selectionModel.setValueIsAdjusting(true);
2126 columnModel.getSelectionModel().setValueIsAdjusting(true);
2127
2128 clearSelection();
2129
2130 selectionModel.setAnchorSelectionIndex(-1);
2131 selectionModel.setLeadSelectionIndex(-1);
2132 columnModel.getSelectionModel().setAnchorSelectionIndex(-1);
2133 columnModel.getSelectionModel().setLeadSelectionIndex(-1);
2134
2135 selectionModel.setValueIsAdjusting(false);
2136 columnModel.getSelectionModel().setValueIsAdjusting(false);
2137 }
2138
2139 private int getAdjustedIndex(int index, boolean row) {
2140 int compare = row ? getRowCount() : getColumnCount();
2141 return index < compare ? index : -1;
2142 }
2143
2144 private int boundRow(int row) throws IllegalArgumentException {
2145 if (row < 0 || row >= getRowCount()) {
2146 throw new IllegalArgumentException("Row index out of range");
2147 }
2148 return row;
2149 }
2150
2151 private int boundColumn(int col) {
2152 if (col< 0 || col >= getColumnCount()) {
2153 throw new IllegalArgumentException("Column index out of range");
2154 }
2155 return col;
2156 }
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168 public void setRowSelectionInterval(int index0, int index1) {
2169 selectionModel.setSelectionInterval(boundRow(index0), boundRow(index1));
2170 }
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182 public void setColumnSelectionInterval(int index0, int index1) {
2183 columnModel.getSelectionModel().setSelectionInterval(boundColumn(index0), boundColumn(index1));
2184 }
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195 public void addRowSelectionInterval(int index0, int index1) {
2196 selectionModel.addSelectionInterval(boundRow(index0), boundRow(index1));
2197 }
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209 public void addColumnSelectionInterval(int index0, int index1) {
2210 columnModel.getSelectionModel().addSelectionInterval(boundColumn(index0), boundColumn(index1));
2211 }
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222 public void removeRowSelectionInterval(int index0, int index1) {
2223 selectionModel.removeSelectionInterval(boundRow(index0), boundRow(index1));
2224 }
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235 public void removeColumnSelectionInterval(int index0, int index1) {
2236 columnModel.getSelectionModel().removeSelectionInterval(boundColumn(index0), boundColumn(index1));
2237 }
2238
2239
2240
2241
2242
2243 public int getSelectedRow() {
2244 return selectionModel.getMinSelectionIndex();
2245 }
2246
2247
2248
2249
2250
2251
2252 public int getSelectedColumn() {
2253 return columnModel.getSelectionModel().getMinSelectionIndex();
2254 }
2255
2256
2257
2258
2259
2260
2261
2262
2263 public int[] getSelectedRows() {
2264 int iMin = selectionModel.getMinSelectionIndex();
2265 int iMax = selectionModel.getMaxSelectionIndex();
2266
2267 if ((iMin == -1) || (iMax == -1)) {
2268 return new int[0];
2269 }
2270
2271 int[] rvTmp = new int[1+ (iMax - iMin)];
2272 int n = 0;
2273 for(int i = iMin; i <= iMax; i++) {
2274 if (selectionModel.isSelectedIndex(i)) {
2275 rvTmp[n++] = i;
2276 }
2277 }
2278 int[] rv = new int[n];
2279 System.arraycopy(rvTmp, 0, rv, 0, n);
2280 return rv;
2281 }
2282
2283
2284
2285
2286
2287
2288
2289
2290 public int[] getSelectedColumns() {
2291 return columnModel.getSelectedColumns();
2292 }
2293
2294
2295
2296
2297
2298
2299 public int getSelectedRowCount() {
2300 int iMin = selectionModel.getMinSelectionIndex();
2301 int iMax = selectionModel.getMaxSelectionIndex();
2302 int count = 0;
2303
2304 for(int i = iMin; i <= iMax; i++) {
2305 if (selectionModel.isSelectedIndex(i)) {
2306 count++;
2307 }
2308 }
2309 return count;
2310 }
2311
2312
2313
2314
2315
2316
2317 public int getSelectedColumnCount() {
2318 return columnModel.getSelectedColumnCount();
2319 }
2320
2321
2322
2323
2324
2325
2326
2327
2328 public boolean isRowSelected(int row) {
2329 return selectionModel.isSelectedIndex(row);
2330 }
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340 public boolean isColumnSelected(int column) {
2341 return columnModel.getSelectionModel().isSelectedIndex(column);
2342 }
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354 public boolean isCellSelected(int row, int column) {
2355 if (!getRowSelectionAllowed() && !getColumnSelectionAllowed()) {
2356 return false;
2357 }
2358 return (!getRowSelectionAllowed() || isRowSelected(row)) &&
2359 (!getColumnSelectionAllowed() || isColumnSelected(column));
2360 }
2361
2362 private void changeSelectionModel(ListSelectionModel sm, int index,
2363 boolean toggle, boolean extend, boolean selected,
2364 int anchor, boolean anchorSelected) {
2365 if (extend) {
2366 if (toggle) {
2367 if (anchorSelected) {
2368 sm.addSelectionInterval(anchor, index);
2369 } else {
2370 sm.removeSelectionInterval(anchor, index);
2371
2372 if (Boolean.TRUE == getClientProperty("Table.isFileList")) {
2373 sm.addSelectionInterval(index, index);
2374 sm.setAnchorSelectionIndex(anchor);
2375 }
2376 }
2377 }
2378 else {
2379 sm.setSelectionInterval(anchor, index);
2380 }
2381 }
2382 else {
2383 if (toggle) {
2384 if (selected) {
2385 sm.removeSelectionInterval(index, index);
2386 }
2387 else {
2388 sm.addSelectionInterval(index, index);
2389 }
2390 }
2391 else {
2392 sm.setSelectionInterval(index, index);
2393 }
2394 }
2395 }
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426 public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
2427 ListSelectionModel rsm = getSelectionModel();
2428 ListSelectionModel csm = getColumnModel().getSelectionModel();
2429
2430 int anchorRow = getAdjustedIndex(rsm.getAnchorSelectionIndex(), true);
2431 int anchorCol = getAdjustedIndex(csm.getAnchorSelectionIndex(), false);
2432
2433 boolean anchorSelected = true;
2434
2435 if (anchorRow == -1) {
2436 if (getRowCount() > 0) {
2437 anchorRow = 0;
2438 }
2439 anchorSelected = false;
2440 }
2441
2442 if (anchorCol == -1) {
2443 if (getColumnCount() > 0) {
2444 anchorCol = 0;
2445 }
2446 anchorSelected = false;
2447 }
2448
2449
2450
2451
2452
2453
2454
2455
2456 boolean selected = isCellSelected(rowIndex, columnIndex);
2457 anchorSelected = anchorSelected && isCellSelected(anchorRow, anchorCol);
2458
2459 changeSelectionModel(csm, columnIndex, toggle, extend, selected,
2460 anchorCol, anchorSelected);
2461 changeSelectionModel(rsm, rowIndex, toggle, extend, selected,
2462 anchorRow, anchorSelected);
2463
2464
2465
2466
2467 if (getAutoscrolls()) {
2468 Rectangle cellRect = getCellRect(rowIndex, columnIndex, false);
2469 if (cellRect != null) {
2470 scrollRectToVisible(cellRect);
2471 }
2472 }
2473 }
2474
2475
2476
2477
2478
2479
2480
2481
2482 public Color getSelectionForeground() {
2483 return selectionForeground;
2484 }
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507 public void setSelectionForeground(Color selectionForeground) {
2508 Color old = this.selectionForeground;
2509 this.selectionForeground = selectionForeground;
2510 firePropertyChange("selectionForeground", old, selectionForeground);
2511 repaint();
2512 }
2513
2514
2515
2516
2517
2518
2519
2520
2521 public Color getSelectionBackground() {
2522 return selectionBackground;
2523 }
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545 public void setSelectionBackground(Color selectionBackground) {
2546 Color old = this.selectionBackground;
2547 this.selectionBackground = selectionBackground;
2548 firePropertyChange("selectionBackground", old, selectionBackground);
2549 repaint();
2550 }
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562 public TableColumn getColumn(Object identifier) {
2563 TableColumnModel cm = getColumnModel();
2564 int columnIndex = cm.getColumnIndex(identifier);
2565 return cm.getColumn(columnIndex);
2566 }
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584 public int convertColumnIndexToModel(int viewColumnIndex) {
2585 return SwingUtilities2.convertColumnIndexToModel(
2586 getColumnModel(), viewColumnIndex);
2587 }
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602 public int convertColumnIndexToView(int modelColumnIndex) {
2603 return SwingUtilities2.convertColumnIndexToView(
2604 getColumnModel(), modelColumnIndex);
2605 }
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620 public int convertRowIndexToView(int modelRowIndex) {
2621 RowSorter sorter = getRowSorter();
2622 if (sorter != null) {
2623 return sorter.convertRowIndexToView(modelRowIndex);
2624 }
2625 return modelRowIndex;
2626 }
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642 public int convertRowIndexToModel(int viewRowIndex) {
2643 RowSorter sorter = getRowSorter();
2644 if (sorter != null) {
2645 return sorter.convertRowIndexToModel(viewRowIndex);
2646 }
2647 return viewRowIndex;
2648 }
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660 public int getRowCount() {
2661 RowSorter sorter = getRowSorter();
2662 if (sorter != null) {
2663 return sorter.getViewRowCount();
2664 }
2665 return getModel().getRowCount();
2666 }
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676 public int getColumnCount() {
2677 return getColumnModel().getColumnCount();
2678 }
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688 public String getColumnName(int column) {
2689 return getModel().getColumnName(convertColumnIndexToModel(column));
2690 }
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700 public Class<?> getColumnClass(int column) {
2701 return getModel().getColumnClass(convertColumnIndexToModel(column));
2702 }
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719 public Object getValueAt(int row, int column) {
2720 return getModel().getValueAt(convertRowIndexToModel(row),
2721 convertColumnIndexToModel(column));
2722 }
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743 public void setValueAt(Object aValue, int row, int column) {
2744 getModel().setValueAt(aValue, convertRowIndexToModel(row),
2745 convertColumnIndexToModel(column));
2746 }
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767 public boolean isCellEditable(int row, int column) {
2768 return getModel().isCellEditable(convertRowIndexToModel(row),
2769 convertColumnIndexToModel(column));
2770 }
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801 public void addColumn(TableColumn aColumn) {
2802 if (aColumn.getHeaderValue() == null) {
2803 int modelColumn = aColumn.getModelIndex();
2804 String columnName = getModel().getColumnName(modelColumn);
2805 aColumn.setHeaderValue(columnName);
2806 }
2807 getColumnModel().addColumn(aColumn);
2808 }
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819 public void removeColumn(TableColumn aColumn) {
2820 getColumnModel().removeColumn(aColumn);
2821 }
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832 public void moveColumn(int column, int targetColumn) {
2833 getColumnModel().moveColumn(column, targetColumn);
2834 }
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851 public int columnAtPoint(Point point) {
2852 int x = point.x;
2853 if( !getComponentOrientation().isLeftToRight() ) {
2854 x = getWidth() - x - 1;
2855 }
2856 return getColumnModel().getColumnIndexAtX(x);
2857 }
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870 public int rowAtPoint(Point point) {
2871 int y = point.y;
2872 int result = (rowModel == null) ? y/getRowHeight() : rowModel.getIndex(y);
2873 if (result < 0) {
2874 return -1;
2875 }
2876 else if (result >= getRowCount()) {
2877 return -1;
2878 }
2879 else {
2880 return result;
2881 }
2882 }
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928 public Rectangle getCellRect(int row, int column, boolean includeSpacing) {
2929 Rectangle r = new Rectangle();
2930 boolean valid = true;
2931 if (row < 0) {
2932
2933 valid = false;
2934 }
2935 else if (row >= getRowCount()) {
2936 r.y = getHeight();
2937 valid = false;
2938 }
2939 else {
2940 r.height = getRowHeight(row);
2941 r.y = (rowModel == null) ? row * r.height : rowModel.getPosition(row);
2942 }
2943
2944 if (column < 0) {
2945 if( !getComponentOrientation().isLeftToRight() ) {
2946 r.x = getWidth();
2947 }
2948
2949 valid = false;
2950 }
2951 else if (column >= getColumnCount()) {
2952 if( getComponentOrientation().isLeftToRight() ) {
2953 r.x = getWidth();
2954 }
2955
2956 valid = false;
2957 }
2958 else {
2959 TableColumnModel cm = getColumnModel();
2960 if( getComponentOrientation().isLeftToRight() ) {
2961 for(int i = 0; i < column; i++) {
2962 r.x += cm.getColumn(i).getWidth();
2963 }
2964 } else {
2965 for(int i = cm.getColumnCount()-1; i > column; i--) {
2966 r.x += cm.getColumn(i).getWidth();
2967 }
2968 }
2969 r.width = cm.getColumn(column).getWidth();
2970 }
2971
2972 if (valid && !includeSpacing) {
2973
2974
2975 int rm = Math.min(getRowMargin(), r.height);
2976 int cm = Math.min(getColumnModel().getColumnMargin(), r.width);
2977
2978 r.setBounds(r.x + cm/2, r.y + rm/2, r.width - cm, r.height - rm);
2979 }
2980 return r;
2981 }
2982
2983 private int viewIndexForColumn(TableColumn aColumn) {
2984 TableColumnModel cm = getColumnModel();
2985 for (int column = 0; column < cm.getColumnCount(); column++) {
2986 if (cm.getColumn(column) == aColumn) {
2987 return column;
2988 }
2989 }
2990 return -1;
2991 }
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128 public void doLayout() {
3129 TableColumn resizingColumn = getResizingColumn();
3130 if (resizingColumn == null) {
3131 setWidthsFromPreferredWidths(false);
3132 }
3133 else {
3134
3135
3136
3137
3138
3139
3140 int columnIndex = viewIndexForColumn(resizingColumn);
3141 int delta = getWidth() - getColumnModel().getTotalColumnWidth();
3142 accommodateDelta(columnIndex, delta);
3143 delta = getWidth() - getColumnModel().getTotalColumnWidth();
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155 if (delta != 0) {
3156 resizingColumn.setWidth(resizingColumn.getWidth() + delta);
3157 }
3158
3159
3160
3161
3162
3163
3164
3165 setWidthsFromPreferredWidths(true);
3166 }
3167
3168 super.doLayout();
3169 }
3170
3171 private TableColumn getResizingColumn() {
3172 return (tableHeader == null) ? null
3173 : tableHeader.getResizingColumn();
3174 }
3175
3176
3177
3178
3179
3180
3181
3182 @Deprecated
3183 public void sizeColumnsToFit(boolean lastColumnOnly) {
3184 int oldAutoResizeMode = autoResizeMode;
3185 setAutoResizeMode(lastColumnOnly ? AUTO_RESIZE_LAST_COLUMN
3186 : AUTO_RESIZE_ALL_COLUMNS);
3187 sizeColumnsToFit(-1);
3188 setAutoResizeMode(oldAutoResizeMode);
3189 }
3190
3191
3192
3193
3194
3195
3196
3197
3198 public void sizeColumnsToFit(int resizingColumn) {
3199 if (resizingColumn == -1) {
3200 setWidthsFromPreferredWidths(false);
3201 }
3202 else {
3203 if (autoResizeMode == AUTO_RESIZE_OFF) {
3204 TableColumn aColumn = getColumnModel().getColumn(resizingColumn);
3205 aColumn.setPreferredWidth(aColumn.getWidth());
3206 }
3207 else {
3208 int delta = getWidth() - getColumnModel().getTotalColumnWidth();
3209 accommodateDelta(resizingColumn, delta);
3210 setWidthsFromPreferredWidths(true);
3211 }
3212 }
3213 }
3214
3215 private void setWidthsFromPreferredWidths(final boolean inverse) {
3216 int totalWidth = getWidth();
3217 int totalPreferred = getPreferredSize().width;
3218 int target = !inverse ? totalWidth : totalPreferred;
3219
3220 final TableColumnModel cm = columnModel;
3221 Resizable3 r = new Resizable3() {
3222 public int getElementCount() { return cm.getColumnCount(); }
3223 public int getLowerBoundAt(int i) { return cm.getColumn(i).getMinWidth(); }
3224 public int getUpperBoundAt(int i) { return cm.getColumn(i).getMaxWidth(); }
3225 public int getMidPointAt(int i) {
3226 if (!inverse) {
3227 return cm.getColumn(i).getPreferredWidth();
3228 }
3229 else {
3230 return cm.getColumn(i).getWidth();
3231 }
3232 }
3233 public void setSizeAt(int s, int i) {
3234 if (!inverse) {
3235 cm.getColumn(i).setWidth(s);
3236 }
3237 else {
3238 cm.getColumn(i).setPreferredWidth(s);
3239 }
3240 }
3241 };
3242
3243 adjustSizes(target, r, inverse);
3244 }
3245
3246
3247
3248 private void accommodateDelta(int resizingColumnIndex, int delta) {
3249 int columnCount = getColumnCount();
3250 int from = resizingColumnIndex;
3251 int to;
3252
3253
3254 switch(autoResizeMode) {
3255 case AUTO_RESIZE_NEXT_COLUMN:
3256 from = from + 1;
3257 to = Math.min(from + 1, columnCount); break;
3258 case AUTO_RESIZE_SUBSEQUENT_COLUMNS:
3259 from = from + 1;
3260 to = columnCount; break;
3261 case AUTO_RESIZE_LAST_COLUMN:
3262 from = columnCount - 1;
3263 to = from + 1; break;
3264 case AUTO_RESIZE_ALL_COLUMNS:
3265 from = 0;
3266 to = columnCount; break;
3267 default:
3268 return;
3269 }
3270
3271 final int start = from;
3272 final int end = to;
3273 final TableColumnModel cm = columnModel;
3274 Resizable3 r = new Resizable3() {
3275 public int getElementCount() { return end-start; }
3276 public int getLowerBoundAt(int i) { return cm.getColumn(i+start).getMinWidth(); }
3277 public int getUpperBoundAt(int i) { return cm.getColumn(i+start).getMaxWidth(); }
3278 public int getMidPointAt(int i) { return cm.getColumn(i+start).getWidth(); }
3279 public void setSizeAt(int s, int i) { cm.getColumn(i+start).setWidth(s); }
3280 };
3281
3282 int totalWidth = 0;
3283 for(int i = from; i < to; i++) {
3284 TableColumn aColumn = columnModel.getColumn(i);
3285 int input = aColumn.getWidth();
3286 totalWidth = totalWidth + input;
3287 }
3288
3289 adjustSizes(totalWidth + delta, r, false);
3290 }
3291
3292 private interface Resizable2 {
3293 public int getElementCount();
3294 public int getLowerBoundAt(int i);
3295 public int getUpperBoundAt(int i);
3296 public void setSizeAt(int newSize, int i);
3297 }
3298
3299 private interface Resizable3 extends Resizable2 {
3300 public int getMidPointAt(int i);
3301 }
3302
3303
3304 private void adjustSizes(long target, final Resizable3 r, boolean inverse) {
3305 int N = r.getElementCount();
3306 long totalPreferred = 0;
3307 for(int i = 0; i < N; i++) {
3308 totalPreferred += r.getMidPointAt(i);
3309 }
3310 Resizable2 s;
3311 if ((target < totalPreferred) == !inverse) {
3312 s = new Resizable2() {
3313 public int getElementCount() { return r.getElementCount(); }
3314 public int getLowerBoundAt(int i) { return r.getLowerBoundAt(i); }
3315 public int getUpperBoundAt(int i) { return r.getMidPointAt(i); }
3316 public void setSizeAt(int newSize, int i) { r.setSizeAt(newSize, i); }
3317
3318 };
3319 }
3320 else {
3321 s = new Resizable2() {
3322 public int getElementCount() { return r.getElementCount(); }
3323 public int getLowerBoundAt(int i) { return r.getMidPointAt(i); }
3324 public int getUpperBoundAt(int i) { return r.getUpperBoundAt(i); }
3325 public void setSizeAt(int newSize, int i) { r.setSizeAt(newSize, i); }
3326
3327 };
3328 }
3329 adjustSizes(target, s, !inverse);
3330 }
3331
3332 private void adjustSizes(long target, Resizable2 r, boolean limitToRange) {
3333 long totalLowerBound = 0;
3334 long totalUpperBound = 0;
3335 for(int i = 0; i < r.getElementCount(); i++) {
3336 totalLowerBound += r.getLowerBoundAt(i);
3337 totalUpperBound += r.getUpperBoundAt(i);
3338 }
3339
3340 if (limitToRange) {
3341 target = Math.min(Math.max(totalLowerBound, target), totalUpperBound);
3342 }
3343
3344 for(int i = 0; i < r.getElementCount(); i++) {
3345 int lowerBound = r.getLowerBoundAt(i);
3346 int upperBound = r.getUpperBoundAt(i);
3347
3348
3349
3350 int newSize;
3351 if (totalLowerBound == totalUpperBound) {
3352 newSize = lowerBound;
3353 }
3354 else {
3355 double f = (double)(target - totalLowerBound)/(totalUpperBound - totalLowerBound);
3356 newSize = (int)Math.round(lowerBound+f*(upperBound - lowerBound));
3357
3358
3359
3360 }
3361 r.setSizeAt(newSize, i);
3362 target -= newSize;
3363 totalLowerBound -= lowerBound;
3364 totalUpperBound -= upperBound;
3365 }
3366 }
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384 public String getToolTipText(MouseEvent event) {
3385 String tip = null;
3386 Point p = event.getPoint();
3387
3388
3389 int hitColumnIndex = columnAtPoint(p);
3390 int hitRowIndex = rowAtPoint(p);
3391
3392 if ((hitColumnIndex != -1) && (hitRowIndex != -1)) {
3393 TableCellRenderer renderer = getCellRenderer(hitRowIndex, hitColumnIndex);
3394 Component component = prepareRenderer(renderer, hitRowIndex, hitColumnIndex);
3395
3396
3397
3398 if (component instanceof JComponent) {
3399
3400 Rectangle cellRect = getCellRect(hitRowIndex, hitColumnIndex, false);
3401 p.translate(-cellRect.x, -cellRect.y);
3402 MouseEvent newEvent = new MouseEvent(component, event.getID(),
3403 event.getWhen(), event.getModifiers(),
3404 p.x, p.y,
3405 event.getXOnScreen(),
3406 event.getYOnScreen(),
3407 event.getClickCount(),
3408 event.isPopupTrigger(),
3409 MouseEvent.NOBUTTON);
3410
3411 tip = ((JComponent)component).getToolTipText(newEvent);
3412 }
3413 }
3414
3415
3416 if (tip == null)
3417 tip = getToolTipText();
3418
3419 return tip;
3420 }
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441 public void setSurrendersFocusOnKeystroke(boolean surrendersFocusOnKeystroke) {
3442 this.surrendersFocusOnKeystroke = surrendersFocusOnKeystroke;
3443 }
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456 public boolean getSurrendersFocusOnKeystroke() {
3457 return surrendersFocusOnKeystroke;
3458 }
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472 public boolean editCellAt(int row, int column) {
3473 return editCellAt(row, column, null);
3474 }
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493 public boolean editCellAt(int row, int column, EventObject e){
3494 if (cellEditor != null && !cellEditor.stopCellEditing()) {
3495 return false;
3496 }
3497
3498 if (row < 0 || row >= getRowCount() ||
3499 column < 0 || column >= getColumnCount()) {
3500 return false;
3501 }
3502
3503 if (!isCellEditable(row, column))
3504 return false;
3505
3506 if (editorRemover == null) {
3507 KeyboardFocusManager fm =
3508 KeyboardFocusManager.getCurrentKeyboardFocusManager();
3509 editorRemover = new CellEditorRemover(fm);
3510 fm.addPropertyChangeListener("permanentFocusOwner", editorRemover);
3511 }
3512
3513 TableCellEditor editor = getCellEditor(row, column);
3514 if (editor != null && editor.isCellEditable(e)) {
3515 editorComp = prepareEditor(editor, row, column);
3516 if (editorComp == null) {
3517 removeEditor();
3518 return false;
3519 }
3520 editorComp.setBounds(getCellRect(row, column, false));
3521 add(editorComp);
3522 editorComp.validate();
3523 editorComp.repaint();
3524
3525 setCellEditor(editor);
3526 setEditingRow(row);
3527 setEditingColumn(column);
3528 editor.addCellEditorListener(this);
3529
3530 return true;
3531 }
3532 return false;
3533 }
3534
3535
3536
3537
3538
3539
3540
3541
3542 public boolean isEditing() {
3543 return cellEditor != null;
3544 }
3545
3546
3547
3548
3549
3550
3551
3552 public Component getEditorComponent() {
3553 return editorComp;
3554 }
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564 public int getEditingColumn() {
3565 return editingColumn;
3566 }
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576 public int getEditingRow() {
3577 return editingRow;
3578 }
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589 public TableUI getUI() {
3590 return (TableUI)ui;
3591 }
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604 public void setUI(TableUI ui) {
3605 if (this.ui != ui) {
3606 super.setUI(ui);
3607 repaint();
3608 }
3609 }
3610
3611
3612
3613
3614
3615
3616
3617
3618 public void updateUI() {
3619
3620 TableColumnModel cm = getColumnModel();
3621 for(int column = 0; column < cm.getColumnCount(); column++) {
3622 TableColumn aColumn = cm.getColumn(column);
3623 SwingUtilities.updateRendererOrEditorUI(aColumn.getCellRenderer());
3624 SwingUtilities.updateRendererOrEditorUI(aColumn.getCellEditor());
3625 SwingUtilities.updateRendererOrEditorUI(aColumn.getHeaderRenderer());
3626 }
3627
3628
3629 Enumeration defaultRenderers = defaultRenderersByColumnClass.elements();
3630 while (defaultRenderers.hasMoreElements()) {
3631 SwingUtilities.updateRendererOrEditorUI(defaultRenderers.nextElement());
3632 }
3633
3634
3635 Enumeration defaultEditors = defaultEditorsByColumnClass.elements();
3636 while (defaultEditors.hasMoreElements()) {
3637 SwingUtilities.updateRendererOrEditorUI(defaultEditors.nextElement());
3638 }
3639
3640
3641 if (tableHeader != null && tableHeader.getParent() == null) {
3642 tableHeader.updateUI();
3643 }
3644
3645
3646 configureEnclosingScrollPaneUI();
3647
3648 setUI((TableUI)UIManager.getUI(this));
3649 }
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659 public String getUIClassID() {
3660 return uiClassID;
3661 }
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679 public void setModel(TableModel dataModel) {
3680 if (dataModel == null) {
3681 throw new IllegalArgumentException("Cannot set a null TableModel");
3682 }
3683 if (this.dataModel != dataModel) {
3684 TableModel old = this.dataModel;
3685 if (old != null) {
3686 old.removeTableModelListener(this);
3687 }
3688 this.dataModel = dataModel;
3689 dataModel.addTableModelListener(this);
3690
3691 tableChanged(new TableModelEvent(dataModel, TableModelEvent.HEADER_ROW));
3692
3693 firePropertyChange("model", old, dataModel);
3694
3695 if (getAutoCreateRowSorter()) {
3696 setRowSorter(new TableRowSorter<TableModel>(dataModel));
3697 }
3698 }
3699 }
3700
3701
3702
3703
3704
3705
3706
3707
3708 public TableModel getModel() {
3709 return dataModel;
3710 }
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724 public void setColumnModel(TableColumnModel columnModel) {
3725 if (columnModel == null) {
3726 throw new IllegalArgumentException("Cannot set a null ColumnModel");
3727 }
3728 TableColumnModel old = this.columnModel;
3729 if (columnModel != old) {
3730 if (old != null) {
3731 old.removeColumnModelListener(this);
3732 }
3733 this.columnModel = columnModel;
3734 columnModel.addColumnModelListener(this);
3735
3736
3737 if (tableHeader != null) {
3738 tableHeader.setColumnModel(columnModel);
3739 }
3740
3741 firePropertyChange("columnModel", old, columnModel);
3742 resizeAndRepaint();
3743 }
3744 }
3745
3746
3747
3748
3749
3750
3751
3752
3753 public TableColumnModel getColumnModel() {
3754 return columnModel;
3755 }
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768 public void setSelectionModel(ListSelectionModel newModel) {
3769 if (newModel == null) {
3770 throw new IllegalArgumentException("Cannot set a null SelectionModel");
3771 }
3772
3773 ListSelectionModel oldModel = selectionModel;
3774
3775 if (newModel != oldModel) {
3776 if (oldModel != null) {
3777 oldModel.removeListSelectionListener(this);
3778 }
3779
3780 selectionModel = newModel;
3781 newModel.addListSelectionListener(this);
3782
3783 firePropertyChange("selectionModel", oldModel, newModel);
3784 repaint();
3785 }
3786 }
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796 public ListSelectionModel getSelectionModel() {
3797 return selectionModel;
3798 }
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812 public void sorterChanged(RowSorterEvent e) {
3813 if (e.getType() == RowSorterEvent.Type.SORT_ORDER_CHANGED) {
3814 JTableHeader header = getTableHeader();
3815 if (header != null) {
3816 header.repaint();
3817 }
3818 }
3819 else if (e.getType() == RowSorterEvent.Type.SORTED) {
3820 sorterChanged = true;
3821 if (!ignoreSortChange) {
3822 sortedTableChanged(e, null);
3823 }
3824 }
3825 }
3826
3827
3828
3829
3830
3831
3832
3833 private final class SortManager {
3834 RowSorter<? extends TableModel> sorter;
3835
3836
3837
3838 private ListSelectionModel modelSelection;
3839 private int modelLeadIndex;
3840
3841
3842 private boolean syncingSelection;
3843
3844
3845 private int[] lastModelSelection;
3846
3847
3848 private SizeSequence modelRowSizes;
3849
3850
3851 SortManager(RowSorter<? extends TableModel> sorter) {
3852 this.sorter = sorter;
3853 sorter.addRowSorterListener(JTable.this);
3854 }
3855
3856
3857
3858
3859 public void dispose() {
3860 if (sorter != null) {
3861 sorter.removeRowSorterListener(JTable.this);
3862 }
3863 }
3864
3865
3866
3867
3868 public void setViewRowHeight(int viewIndex, int rowHeight) {
3869 if (modelRowSizes == null) {
3870 modelRowSizes = new SizeSequence(getModel().getRowCount(),
3871 getRowHeight());
3872 }
3873 modelRowSizes.setSize(convertRowIndexToModel(viewIndex),rowHeight);
3874 }
3875
3876
3877
3878
3879 public void allChanged() {
3880 modelLeadIndex = -1;
3881 modelSelection = null;
3882 modelRowSizes = null;
3883 }
3884
3885
3886
3887
3888 public void viewSelectionChanged(ListSelectionEvent e) {
3889 if (!syncingSelection && modelSelection != null) {
3890 modelSelection = null;
3891 }
3892 }
3893
3894
3895
3896
3897
3898
3899 public void prepareForChange(RowSorterEvent sortEvent,
3900 ModelChange change) {
3901 if (getUpdateSelectionOnSort()) {
3902 cacheSelection(sortEvent, change);
3903 }
3904 }
3905
3906
3907
3908
3909 private void cacheSelection(RowSorterEvent sortEvent,
3910 ModelChange change) {
3911 if (sortEvent != null) {
3912
3913
3914
3915
3916 if (modelSelection == null &&
3917 sorter.getViewRowCount() != getModel().getRowCount()) {
3918 modelSelection = new DefaultListSelectionModel();
3919 ListSelectionModel viewSelection = getSelectionModel();
3920 int min = viewSelection.getMinSelectionIndex();
3921 int max = viewSelection.getMaxSelectionIndex();
3922 int modelIndex;
3923 for (int viewIndex = min; viewIndex <= max; viewIndex++) {
3924 if (viewSelection.isSelectedIndex(viewIndex)) {
3925 modelIndex = convertRowIndexToModel(
3926 sortEvent, viewIndex);
3927 if (modelIndex != -1) {
3928 modelSelection.addSelectionInterval(
3929 modelIndex, modelIndex);
3930 }
3931 }
3932 }
3933 modelIndex = convertRowIndexToModel(sortEvent,
3934 viewSelection.getLeadSelectionIndex());
3935 SwingUtilities2.setLeadAnchorWithoutSelection(
3936 modelSelection, modelIndex, modelIndex);
3937 } else if (modelSelection == null) {
3938
3939
3940 cacheModelSelection(sortEvent);
3941 }
3942 } else if (change.allRowsChanged) {
3943
3944 modelSelection = null;
3945 } else if (modelSelection != null) {
3946
3947 switch(change.type) {
3948 case TableModelEvent.DELETE:
3949 modelSelection.removeIndexInterval(change.startModelIndex,
3950 change.endModelIndex);
3951 break;
3952 case TableModelEvent.INSERT:
3953 modelSelection.insertIndexInterval(change.startModelIndex,
3954 change.endModelIndex,
3955 true);
3956 break;
3957 default:
3958 break;
3959 }
3960 } else {
3961
3962
3963 cacheModelSelection(null);
3964 }
3965 }
3966
3967 private void cacheModelSelection(RowSorterEvent sortEvent) {
3968 lastModelSelection = convertSelectionToModel(sortEvent);
3969 modelLeadIndex = convertRowIndexToModel(sortEvent,
3970 selectionModel.getLeadSelectionIndex());
3971 }
3972
3973
3974
3975
3976
3977
3978 public void processChange(RowSorterEvent sortEvent,
3979 ModelChange change,
3980 boolean sorterChanged) {
3981 if (change != null) {
3982 if (change.allRowsChanged) {
3983 modelRowSizes = null;
3984 rowModel = null;
3985 } else if (modelRowSizes != null) {
3986 if (change.type == TableModelEvent.INSERT) {
3987 modelRowSizes.insertEntries(change.startModelIndex,
3988 change.endModelIndex -
3989 change.startModelIndex + 1,
3990 getRowHeight());
3991 } else if (change.type == TableModelEvent.DELETE) {
3992 modelRowSizes.removeEntries(change.startModelIndex,
3993 change.endModelIndex -
3994 change.startModelIndex +1 );
3995 }
3996 }
3997 }
3998 if (sorterChanged) {
3999 setViewRowHeightsFromModel();
4000 restoreSelection(change);
4001 }
4002 }
4003
4004
4005
4006
4007
4008 private void setViewRowHeightsFromModel() {
4009 if (modelRowSizes != null) {
4010 rowModel.setSizes(getRowCount(), getRowHeight());
4011 for (int viewIndex = getRowCount() - 1; viewIndex >= 0;
4012 viewIndex--) {
4013 int modelIndex = convertRowIndexToModel(viewIndex);
4014 rowModel.setSize(viewIndex,
4015 modelRowSizes.getSize(modelIndex));
4016 }
4017 }
4018 }
4019
4020
4021
4022
4023 private void restoreSelection(ModelChange change) {
4024 syncingSelection = true;
4025 if (lastModelSelection != null) {
4026 restoreSortingSelection(lastModelSelection,
4027 modelLeadIndex, change);
4028 lastModelSelection = null;
4029 } else if (modelSelection != null) {
4030 ListSelectionModel viewSelection = getSelectionModel();
4031 viewSelection.setValueIsAdjusting(true);
4032 viewSelection.clearSelection();
4033 int min = modelSelection.getMinSelectionIndex();
4034 int max = modelSelection.getMaxSelectionIndex();
4035 int viewIndex;
4036 for (int modelIndex = min; modelIndex <= max; modelIndex++) {
4037 if (modelSelection.isSelectedIndex(modelIndex)) {
4038 viewIndex = convertRowIndexToView(modelIndex);
4039 if (viewIndex != -1) {
4040 viewSelection.addSelectionInterval(viewIndex,
4041 viewIndex);
4042 }
4043 }
4044 }
4045
4046 int viewLeadIndex = modelSelection.getLeadSelectionIndex();
4047 if (viewLeadIndex != -1) {
4048 viewLeadIndex = convertRowIndexToView(viewLeadIndex);
4049 }
4050 SwingUtilities2.setLeadAnchorWithoutSelection(
4051 viewSelection, viewLeadIndex, viewLeadIndex);
4052 viewSelection.setValueIsAdjusting(false);
4053 }
4054 syncingSelection = false;
4055 }
4056 }
4057
4058
4059
4060
4061
4062
4063
4064 private final class ModelChange {
4065
4066 int startModelIndex;
4067
4068
4069 int endModelIndex;
4070
4071
4072 int type;
4073
4074
4075 int modelRowCount;
4076
4077
4078 TableModelEvent event;
4079
4080
4081 int length;
4082
4083
4084 boolean allRowsChanged;
4085
4086 ModelChange(TableModelEvent e) {
4087 startModelIndex = Math.max(0, e.getFirstRow());
4088 endModelIndex = e.getLastRow();
4089 modelRowCount = getModel().getRowCount();
4090 if (endModelIndex < 0) {
4091 endModelIndex = Math.max(0, modelRowCount - 1);
4092 }
4093 length = endModelIndex - startModelIndex + 1;
4094 type = e.getType();
4095 event = e;
4096 allRowsChanged = (e.getLastRow() == Integer.MAX_VALUE);
4097 }
4098 }
4099
4100
4101
4102
4103
4104 private void sortedTableChanged(RowSorterEvent sortedEvent,
4105 TableModelEvent e) {
4106 int editingModelIndex = -1;
4107 ModelChange change = (e != null) ? new ModelChange(e) : null;
4108
4109 if ((change == null || !change.allRowsChanged) &&
4110 this.editingRow != -1) {
4111 editingModelIndex = convertRowIndexToModel(sortedEvent,
4112 this.editingRow);
4113 }
4114
4115 sortManager.prepareForChange(sortedEvent, change);
4116
4117 if (e != null) {
4118 if (change.type == TableModelEvent.UPDATE) {
4119 repaintSortedRows(change);
4120 }
4121 notifySorter(change);
4122 if (change.type != TableModelEvent.UPDATE) {
4123
4124
4125 sorterChanged = true;
4126 }
4127 }
4128 else {
4129 sorterChanged = true;
4130 }
4131
4132 sortManager.processChange(sortedEvent, change, sorterChanged);
4133
4134 if (sorterChanged) {
4135
4136 if (this.editingRow != -1) {
4137 int newIndex = (editingModelIndex == -1) ? -1 :
4138 convertRowIndexToView(editingModelIndex,change);
4139 restoreSortingEditingRow(newIndex);
4140 }
4141
4142
4143 if (e == null || change.type != TableModelEvent.UPDATE) {
4144 resizeAndRepaint();
4145 }
4146 }
4147
4148
4149 if (change != null && change.allRowsChanged) {
4150 clearSelectionAndLeadAnchor();
4151 resizeAndRepaint();
4152 }
4153 }
4154
4155
4156
4157
4158 private void repaintSortedRows(ModelChange change) {
4159 if (change.startModelIndex > change.endModelIndex ||
4160 change.startModelIndex + 10 < change.endModelIndex) {
4161
4162 repaint();
4163 return;
4164 }
4165 int eventColumn = change.event.getColumn();
4166 int columnViewIndex = eventColumn;
4167 if (columnViewIndex == TableModelEvent.ALL_COLUMNS) {
4168 columnViewIndex = 0;
4169 }
4170 else {
4171 columnViewIndex = convertColumnIndexToView(columnViewIndex);
4172 if (columnViewIndex == -1) {
4173 return;
4174 }
4175 }
4176 int modelIndex = change.startModelIndex;
4177 while (modelIndex <= change.endModelIndex) {
4178 int viewIndex = convertRowIndexToView(modelIndex++);
4179 if (viewIndex != -1) {
4180 Rectangle dirty = getCellRect(viewIndex, columnViewIndex,
4181 false);
4182 int x = dirty.x;
4183 int w = dirty.width;
4184 if (eventColumn == TableModelEvent.ALL_COLUMNS) {
4185 x = 0;
4186 w = getWidth();
4187 }
4188 repaint(x, dirty.y, w, dirty.height);
4189 }
4190 }
4191 }
4192
4193
4194
4195
4196
4197 private void restoreSortingSelection(int[] selection, int lead,
4198 ModelChange change) {
4199
4200 for (int i = selection.length - 1; i >= 0; i--) {
4201 selection[i] = convertRowIndexToView(selection[i], change);
4202 }
4203 lead = convertRowIndexToView(lead, change);
4204
4205
4206 if (selection.length == 0 ||
4207 (selection.length == 1 && selection[0] == getSelectedRow())) {
4208 return;
4209 }
4210
4211
4212 selectionModel.setValueIsAdjusting(true);
4213 selectionModel.clearSelection();
4214 for (int i = selection.length - 1; i >= 0; i--) {
4215 if (selection[i] != -1) {
4216 selectionModel.addSelectionInterval(selection[i],
4217 selection[i]);
4218 }
4219 }
4220 SwingUtilities2.setLeadAnchorWithoutSelection(
4221 selectionModel, lead, lead);
4222 selectionModel.setValueIsAdjusting(false);
4223 }
4224
4225
4226
4227
4228
4229
4230 private void restoreSortingEditingRow(int editingRow) {
4231 if (editingRow == -1) {
4232
4233 TableCellEditor editor = getCellEditor();
4234 if (editor != null) {
4235
4236 editor.cancelCellEditing();
4237 if (getCellEditor() != null) {
4238
4239
4240 removeEditor();
4241 }
4242 }
4243 }
4244 else {
4245
4246 this.editingRow = editingRow;
4247 repaint();
4248 }
4249 }
4250
4251
4252
4253
4254 private void notifySorter(ModelChange change) {
4255 try {
4256 ignoreSortChange = true;
4257 sorterChanged = false;
4258 switch(change.type) {
4259 case TableModelEvent.UPDATE:
4260 if (change.event.getLastRow() == Integer.MAX_VALUE) {
4261 sortManager.sorter.allRowsChanged();
4262 } else if (change.event.getColumn() ==
4263 TableModelEvent.ALL_COLUMNS) {
4264 sortManager.sorter.rowsUpdated(change.startModelIndex,
4265 change.endModelIndex);
4266 } else {
4267 sortManager.sorter.rowsUpdated(change.startModelIndex,
4268 change.endModelIndex,
4269 change.event.getColumn());
4270 }
4271 break;
4272 case TableModelEvent.INSERT:
4273 sortManager.sorter.rowsInserted(change.startModelIndex,
4274 change.endModelIndex);
4275 break;
4276 case TableModelEvent.DELETE:
4277 sortManager.sorter.rowsDeleted(change.startModelIndex,
4278 change.endModelIndex);
4279 break;
4280 }
4281 } finally {
4282 ignoreSortChange = false;
4283 }
4284 }
4285
4286
4287
4288
4289
4290
4291
4292
4293 private int convertRowIndexToView(int modelIndex, ModelChange change) {
4294 if (modelIndex < 0) {
4295 return -1;
4296 }
4297 if (change != null && modelIndex >= change.startModelIndex) {
4298 if (change.type == TableModelEvent.INSERT) {
4299 if (modelIndex + change.length >= change.modelRowCount) {
4300 return -1;
4301 }
4302 return sortManager.sorter.convertRowIndexToView(
4303 modelIndex + change.length);
4304 }
4305 else if (change.type == TableModelEvent.DELETE) {
4306 if (modelIndex <= change.endModelIndex) {
4307
4308 return -1;
4309 }
4310 else {
4311 if (modelIndex - change.length >= change.modelRowCount) {
4312 return -1;
4313 }
4314 return sortManager.sorter.convertRowIndexToView(
4315 modelIndex - change.length);
4316 }
4317 }
4318
4319 }
4320 if (modelIndex >= getModel().getRowCount()) {
4321 return -1;
4322 }
4323 return sortManager.sorter.convertRowIndexToView(modelIndex);
4324 }
4325
4326
4327
4328
4329
4330 private int[] convertSelectionToModel(RowSorterEvent e) {
4331 int[] selection = getSelectedRows();
4332 for (int i = selection.length - 1; i >= 0; i--) {
4333 selection[i] = convertRowIndexToModel(e, selection[i]);
4334 }
4335 return selection;
4336 }
4337
4338 private int convertRowIndexToModel(RowSorterEvent e, int viewIndex) {
4339 if (e != null) {
4340 if (e.getPreviousRowCount() == 0) {
4341 return viewIndex;
4342 }
4343
4344 return e.convertPreviousRowIndexToModel(viewIndex);
4345 }
4346
4347 if (viewIndex < 0 || viewIndex >= getRowCount()) {
4348 return -1;
4349 }
4350 return convertRowIndexToModel(viewIndex);
4351 }
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370 public void tableChanged(TableModelEvent e) {
4371 if (e == null || e.getFirstRow() == TableModelEvent.HEADER_ROW) {
4372
4373 clearSelectionAndLeadAnchor();
4374
4375 rowModel = null;
4376
4377 if (sortManager != null) {
4378 try {
4379 ignoreSortChange = true;
4380 sortManager.sorter.modelStructureChanged();
4381 } finally {
4382 ignoreSortChange = false;
4383 }
4384 sortManager.allChanged();
4385 }
4386
4387 if (getAutoCreateColumnsFromModel()) {
4388
4389 createDefaultColumnsFromModel();
4390 return;
4391 }
4392
4393 resizeAndRepaint();
4394 return;
4395 }
4396
4397 if (sortManager != null) {
4398 sortedTableChanged(null, e);
4399 return;
4400 }
4401
4402
4403
4404
4405 if (rowModel != null) {
4406 repaint();
4407 }
4408
4409 if (e.getType() == TableModelEvent.INSERT) {
4410 tableRowsInserted(e);
4411 return;
4412 }
4413
4414 if (e.getType() == TableModelEvent.DELETE) {
4415 tableRowsDeleted(e);
4416 return;
4417 }
4418
4419 int modelColumn = e.getColumn();
4420 int start = e.getFirstRow();
4421 int end = e.getLastRow();
4422
4423 Rectangle dirtyRegion;
4424 if (modelColumn == TableModelEvent.ALL_COLUMNS) {
4425
4426 dirtyRegion = new Rectangle(0, start * getRowHeight(),
4427 getColumnModel().getTotalColumnWidth(), 0);
4428 }
4429 else {
4430
4431
4432
4433
4434
4435 int column = convertColumnIndexToView(modelColumn);
4436 dirtyRegion = getCellRect(start, column, false);
4437 }
4438
4439
4440
4441 if (end != Integer.MAX_VALUE) {
4442 dirtyRegion.height = (end-start+1)*getRowHeight();
4443 repaint(dirtyRegion.x, dirtyRegion.y, dirtyRegion.width, dirtyRegion.height);
4444 }
4445
4446
4447 else {
4448 clearSelectionAndLeadAnchor();
4449 resizeAndRepaint();
4450 rowModel = null;
4451 }
4452 }
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462 private void tableRowsInserted(TableModelEvent e) {
4463 int start = e.getFirstRow();
4464 int end = e.getLastRow();
4465 if (start < 0) {
4466 start = 0;
4467 }
4468 if (end < 0) {
4469 end = getRowCount()-1;
4470 }
4471
4472
4473 int length = end - start + 1;
4474 selectionModel.insertIndexInterval(start, length, true);
4475
4476
4477 if (rowModel != null) {
4478 rowModel.insertEntries(start, length, getRowHeight());
4479 }
4480 int rh = getRowHeight() ;
4481 Rectangle drawRect = new Rectangle(0, start * rh,
4482 getColumnModel().getTotalColumnWidth(),
4483 (getRowCount()-start) * rh);
4484
4485 revalidate();
4486
4487
4488 repaint(drawRect);
4489 }
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499 private void tableRowsDeleted(TableModelEvent e) {
4500 int start = e.getFirstRow();
4501 int end = e.getLastRow();
4502 if (start < 0) {
4503 start = 0;
4504 }
4505 if (end < 0) {
4506 end = getRowCount()-1;
4507 }
4508
4509 int deletedCount = end - start + 1;
4510 int previousRowCount = getRowCount() + deletedCount;
4511
4512 selectionModel.removeIndexInterval(start, end);
4513
4514
4515 if (rowModel != null) {
4516 rowModel.removeEntries(start, deletedCount);
4517 }
4518
4519 int rh = getRowHeight();
4520 Rectangle drawRect = new Rectangle(0, start * rh,
4521 getColumnModel().getTotalColumnWidth(),
4522 (previousRowCount - start) * rh);
4523
4524 revalidate();
4525
4526
4527 repaint(drawRect);
4528 }
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542 public void columnAdded(TableColumnModelEvent e) {
4543
4544 if (isEditing()) {
4545 removeEditor();
4546 }
4547 resizeAndRepaint();
4548 }
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558 public void columnRemoved(TableColumnModelEvent e) {
4559
4560 if (isEditing()) {
4561 removeEditor();
4562 }
4563 resizeAndRepaint();
4564 }
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576 public void columnMoved(TableColumnModelEvent e) {
4577 if (isEditing() && !getCellEditor().stopCellEditing()) {
4578 getCellEditor().cancelCellEditing();
4579 }
4580 repaint();
4581 }
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594 public void columnMarginChanged(ChangeEvent e) {
4595 if (isEditing() && !getCellEditor().stopCellEditing()) {
4596 getCellEditor().cancelCellEditing();
4597 }
4598 TableColumn resizingColumn = getResizingColumn();
4599
4600
4601 if (resizingColumn != null && autoResizeMode == AUTO_RESIZE_OFF) {
4602 resizingColumn.setPreferredWidth(resizingColumn.getWidth());
4603 }
4604 resizeAndRepaint();
4605 }
4606
4607 private int limit(int i, int a, int b) {
4608 return Math.min(b, Math.max(i, a));
4609 }
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621 public void columnSelectionChanged(ListSelectionEvent e) {
4622 boolean isAdjusting = e.getValueIsAdjusting();
4623 if (columnSelectionAdjusting && !isAdjusting) {
4624
4625
4626
4627 columnSelectionAdjusting = false;
4628 return;
4629 }
4630 columnSelectionAdjusting = isAdjusting;
4631
4632 if (getRowCount() <= 0 || getColumnCount() <= 0) {
4633 return;
4634 }
4635 int firstIndex = limit(e.getFirstIndex(), 0, getColumnCount()-1);
4636 int lastIndex = limit(e.getLastIndex(), 0, getColumnCount()-1);
4637 int minRow = 0;
4638 int maxRow = getRowCount() - 1;
4639 if (getRowSelectionAllowed()) {
4640 minRow = selectionModel.getMinSelectionIndex();
4641 maxRow = selectionModel.getMaxSelectionIndex();
4642 int leadRow = getAdjustedIndex(selectionModel.getLeadSelectionIndex(), true);
4643
4644 if (minRow == -1 || maxRow == -1) {
4645 if (leadRow == -1) {
4646
4647 return;
4648 }
4649
4650
4651 minRow = maxRow = leadRow;
4652 } else {
4653
4654
4655
4656 if (leadRow != -1) {
4657 minRow = Math.min(minRow, leadRow);
4658 maxRow = Math.max(maxRow, leadRow);
4659 }
4660 }
4661 }
4662 Rectangle firstColumnRect = getCellRect(minRow, firstIndex, false);
4663 Rectangle lastColumnRect = getCellRect(maxRow, lastIndex, false);
4664 Rectangle dirtyRegion = firstColumnRect.union(lastColumnRect);
4665 repaint(dirtyRegion);
4666 }
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682 public void valueChanged(ListSelectionEvent e) {
4683 if (sortManager != null) {
4684 sortManager.viewSelectionChanged(e);
4685 }
4686 boolean isAdjusting = e.getValueIsAdjusting();
4687 if (rowSelectionAdjusting && !isAdjusting) {
4688
4689
4690
4691 rowSelectionAdjusting = false;
4692 return;
4693 }
4694 rowSelectionAdjusting = isAdjusting;
4695
4696 if (getRowCount() <= 0 || getColumnCount() <= 0) {
4697 return;
4698 }
4699 int firstIndex = limit(e.getFirstIndex(), 0, getRowCount()-1);
4700 int lastIndex = limit(e.getLastIndex(), 0, getRowCount()-1);
4701 Rectangle firstRowRect = getCellRect(firstIndex, 0, false);
4702 Rectangle lastRowRect = getCellRect(lastIndex, getColumnCount()-1, false);
4703 Rectangle dirtyRegion = firstRowRect.union(lastRowRect);
4704 repaint(dirtyRegion);
4705 }
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721 public void editingStopped(ChangeEvent e) {
4722
4723 TableCellEditor editor = getCellEditor();
4724 if (editor != null) {
4725 Object value = editor.getCellEditorValue();
4726 setValueAt(value, editingRow, editingColumn);
4727 removeEditor();
4728 }
4729 }
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741 public void editingCanceled(ChangeEvent e) {
4742 removeEditor();
4743 }
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758 public void setPreferredScrollableViewportSize(Dimension size) {
4759 preferredViewportSize = size;
4760 }
4761
4762
4763
4764
4765
4766
4767
4768
4769 public Dimension getPreferredScrollableViewportSize() {
4770 return preferredViewportSize;
4771 }
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787 public int getScrollableUnitIncrement(Rectangle visibleRect,
4788 int orientation,
4789 int direction) {
4790 int leadingRow;
4791 int leadingCol;
4792 Rectangle leadingCellRect;
4793
4794 int leadingVisibleEdge;
4795 int leadingCellEdge;
4796 int leadingCellSize;
4797
4798 leadingRow = getLeadingRow(visibleRect);
4799 leadingCol = getLeadingCol(visibleRect);
4800 if (orientation == SwingConstants.VERTICAL && leadingRow < 0) {
4801
4802 return getRowHeight();
4803 }
4804 else if (orientation == SwingConstants.HORIZONTAL && leadingCol < 0) {
4805
4806 return 100;
4807 }
4808
4809
4810
4811
4812 leadingCellRect = getCellRect(leadingRow, leadingCol, true);
4813 leadingVisibleEdge = leadingEdge(visibleRect, orientation);
4814 leadingCellEdge = leadingEdge(leadingCellRect, orientation);
4815
4816 if (orientation == SwingConstants.VERTICAL) {
4817 leadingCellSize = leadingCellRect.height;
4818
4819 }
4820 else {
4821 leadingCellSize = leadingCellRect.width;
4822 }
4823
4824
4825
4826
4827
4828
4829
4830 if (leadingVisibleEdge == leadingCellEdge) {
4831
4832
4833 if (direction < 0) {
4834 int retVal = 0;
4835
4836 if (orientation == SwingConstants.VERTICAL) {
4837
4838 while (--leadingRow >= 0) {
4839 retVal = getRowHeight(leadingRow);
4840 if (retVal != 0) {
4841 break;
4842 }
4843 }
4844 }
4845 else {
4846
4847 while (--leadingCol >= 0) {
4848 retVal = getCellRect(leadingRow, leadingCol, true).width;
4849 if (retVal != 0) {
4850 break;
4851 }
4852 }
4853 }
4854 return retVal;
4855 }
4856 else {
4857 return leadingCellSize;
4858 }
4859 }
4860 else {
4861
4862 int hiddenAmt = Math.abs(leadingVisibleEdge - leadingCellEdge);
4863 int visibleAmt = leadingCellSize - hiddenAmt;
4864
4865 if (direction > 0) {
4866
4867 return visibleAmt;
4868 }
4869 else {
4870 return hiddenAmt;
4871 }
4872 }
4873 }
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888 public int getScrollableBlockIncrement(Rectangle visibleRect,
4889 int orientation, int direction) {
4890
4891 if (getRowCount() == 0) {
4892
4893 if (SwingConstants.VERTICAL == orientation) {
4894 int rh = getRowHeight();
4895 return (rh > 0) ? Math.max(rh, (visibleRect.height / rh) * rh) :
4896 visibleRect.height;
4897 }
4898 else {
4899 return visibleRect.width;
4900 }
4901 }
4902
4903 if (null == rowModel && SwingConstants.VERTICAL == orientation) {
4904 int row = rowAtPoint(visibleRect.getLocation());
4905 assert row != -1;
4906 int col = columnAtPoint(visibleRect.getLocation());
4907 Rectangle cellRect = getCellRect(row, col, true);
4908
4909 if (cellRect.y == visibleRect.y) {
4910 int rh = getRowHeight();
4911 assert rh > 0;
4912 return Math.max(rh, (visibleRect.height / rh) * rh);
4913 }
4914 }
4915 if (direction < 0) {
4916 return getPreviousBlockIncrement(visibleRect, orientation);
4917 }
4918 else {
4919 return getNextBlockIncrement(visibleRect, orientation);
4920 }
4921 }
4922
4923
4924
4925
4926
4927
4928 private int getPreviousBlockIncrement(Rectangle visibleRect,
4929 int orientation) {
4930
4931
4932
4933
4934 int row;
4935 int col;
4936
4937 int newEdge;
4938 Point newCellLoc;
4939
4940 int visibleLeadingEdge = leadingEdge(visibleRect, orientation);
4941 boolean leftToRight = getComponentOrientation().isLeftToRight();
4942 int newLeadingEdge;
4943
4944
4945
4946
4947 if (orientation == SwingConstants.VERTICAL) {
4948 newEdge = visibleLeadingEdge - visibleRect.height;
4949 int x = visibleRect.x + (leftToRight ? 0 : visibleRect.width);
4950 newCellLoc = new Point(x, newEdge);
4951 }
4952 else if (leftToRight) {
4953 newEdge = visibleLeadingEdge - visibleRect.width;
4954 newCellLoc = new Point(newEdge, visibleRect.y);
4955 }
4956 else {
4957 newEdge = visibleLeadingEdge + visibleRect.width;
4958 newCellLoc = new Point(newEdge - 1, visibleRect.y);
4959 }
4960 row = rowAtPoint(newCellLoc);
4961 col = columnAtPoint(newCellLoc);
4962
4963
4964
4965 if (orientation == SwingConstants.VERTICAL & row < 0) {
4966 newLeadingEdge = 0;
4967 }
4968 else if (orientation == SwingConstants.HORIZONTAL & col < 0) {
4969 if (leftToRight) {
4970 newLeadingEdge = 0;
4971 }
4972 else {
4973 newLeadingEdge = getWidth();
4974 }
4975 }
4976 else {
4977
4978 Rectangle newCellRect = getCellRect(row, col, true);
4979 int newCellLeadingEdge = leadingEdge(newCellRect, orientation);
4980 int newCellTrailingEdge = trailingEdge(newCellRect, orientation);
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992 if ((orientation == SwingConstants.VERTICAL || leftToRight) &&
4993 (newCellTrailingEdge >= visibleLeadingEdge)) {
4994 newLeadingEdge = newCellLeadingEdge;
4995 }
4996 else if (orientation == SwingConstants.HORIZONTAL &&
4997 !leftToRight &&
4998 newCellTrailingEdge <= visibleLeadingEdge) {
4999 newLeadingEdge = newCellLeadingEdge;
5000 }
5001
5002 else if (newEdge == newCellLeadingEdge) {
5003 newLeadingEdge = newCellLeadingEdge;
5004 }
5005
5006 else {
5007 newLeadingEdge = newCellTrailingEdge;
5008 }
5009 }
5010 return Math.abs(visibleLeadingEdge - newLeadingEdge);
5011 }
5012
5013
5014
5015
5016
5017
5018 private int getNextBlockIncrement(Rectangle visibleRect,
5019 int orientation) {
5020
5021
5022 int trailingRow = getTrailingRow(visibleRect);
5023 int trailingCol = getTrailingCol(visibleRect);
5024
5025 Rectangle cellRect;
5026 boolean cellFillsVis;
5027
5028 int cellLeadingEdge;
5029 int cellTrailingEdge;
5030 int newLeadingEdge;
5031 int visibleLeadingEdge = leadingEdge(visibleRect, orientation);
5032
5033
5034
5035
5036
5037
5038
5039 if (orientation == SwingConstants.VERTICAL && trailingRow < 0) {
5040 return visibleRect.height;
5041 }
5042 else if (orientation == SwingConstants.HORIZONTAL && trailingCol < 0) {
5043 return visibleRect.width;
5044 }
5045 cellRect = getCellRect(trailingRow, trailingCol, true);
5046 cellLeadingEdge = leadingEdge(cellRect, orientation);
5047 cellTrailingEdge = trailingEdge(cellRect, orientation);
5048
5049 if (orientation == SwingConstants.VERTICAL ||
5050 getComponentOrientation().isLeftToRight()) {
5051 cellFillsVis = cellLeadingEdge <= visibleLeadingEdge;
5052 }
5053 else {
5054 cellFillsVis = cellLeadingEdge >= visibleLeadingEdge;
5055 }
5056
5057 if (cellFillsVis) {
5058
5059
5060 newLeadingEdge = cellTrailingEdge;
5061 }
5062 else if (cellTrailingEdge == trailingEdge(visibleRect, orientation)) {
5063
5064
5065 newLeadingEdge = cellTrailingEdge;
5066 }
5067 else {
5068
5069
5070
5071 newLeadingEdge = cellLeadingEdge;
5072 }
5073 return Math.abs(newLeadingEdge - visibleLeadingEdge);
5074 }
5075
5076
5077
5078
5079
5080
5081 private int getLeadingRow(Rectangle visibleRect) {
5082 Point leadingPoint;
5083
5084 if (getComponentOrientation().isLeftToRight()) {
5085 leadingPoint = new Point(visibleRect.x, visibleRect.y);
5086 }
5087 else {
5088 leadingPoint = new Point(visibleRect.x + visibleRect.width - 1,
5089 visibleRect.y);
5090 }
5091 return rowAtPoint(leadingPoint);
5092 }
5093
5094
5095
5096
5097
5098
5099 private int getLeadingCol(Rectangle visibleRect) {
5100 Point leadingPoint;
5101
5102 if (getComponentOrientation().isLeftToRight()) {
5103 leadingPoint = new Point(visibleRect.x, visibleRect.y);
5104 }
5105 else {
5106 leadingPoint = new Point(visibleRect.x + visibleRect.width - 1,
5107 visibleRect.y);
5108 }
5109 return columnAtPoint(leadingPoint);
5110 }
5111
5112
5113
5114
5115
5116
5117 private int getTrailingRow(Rectangle visibleRect) {
5118 Point trailingPoint;
5119
5120 if (getComponentOrientation().isLeftToRight()) {
5121 trailingPoint = new Point(visibleRect.x,
5122 visibleRect.y + visibleRect.height - 1);
5123 }
5124 else {
5125 trailingPoint = new Point(visibleRect.x + visibleRect.width - 1,
5126 visibleRect.y + visibleRect.height - 1);
5127 }
5128 return rowAtPoint(trailingPoint);
5129 }
5130
5131
5132
5133
5134
5135
5136 private int getTrailingCol(Rectangle visibleRect) {
5137 Point trailingPoint;
5138
5139 if (getComponentOrientation().isLeftToRight()) {
5140 trailingPoint = new Point(visibleRect.x + visibleRect.width - 1,
5141 visibleRect.y);
5142 }
5143 else {
5144 trailingPoint = new Point(visibleRect.x, visibleRect.y);
5145 }
5146 return columnAtPoint(trailingPoint);
5147 }
5148
5149
5150
5151
5152
5153
5154 private int leadingEdge(Rectangle rect, int orientation) {
5155 if (orientation == SwingConstants.VERTICAL) {
5156 return rect.y;
5157 }
5158 else if (getComponentOrientation().isLeftToRight()) {
5159 return rect.x;
5160 }
5161 else {
5162 return rect.x + rect.width;
5163 }
5164 }
5165
5166
5167
5168
5169
5170
5171 private int trailingEdge(Rectangle rect, int orientation) {
5172 if (orientation == SwingConstants.VERTICAL) {
5173 return rect.y + rect.height;
5174 }
5175 else if (getComponentOrientation().isLeftToRight()) {
5176 return rect.x + rect.width;
5177 }
5178 else {
5179 return rect.x;
5180 }
5181 }
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193 public boolean getScrollableTracksViewportWidth() {
5194 return !(autoResizeMode == AUTO_RESIZE_OFF);
5195 }
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210 public boolean getScrollableTracksViewportHeight() {
5211 Container parent = SwingUtilities.getUnwrappedParent(this);
5212 return getFillsViewportHeight()
5213 && parent instanceof JViewport
5214 && parent.getHeight() > getPreferredSize().height;
5215 }
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236 public void setFillsViewportHeight(boolean fillsViewportHeight) {
5237 boolean old = this.fillsViewportHeight;
5238 this.fillsViewportHeight = fillsViewportHeight;
5239 resizeAndRepaint();
5240 firePropertyChange("fillsViewportHeight", old, fillsViewportHeight);
5241 }
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252 public boolean getFillsViewportHeight() {
5253 return fillsViewportHeight;
5254 }
5255
5256
5257
5258
5259
5260 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
5261 int condition, boolean pressed) {
5262 boolean retValue = super.processKeyBinding(ks, e, condition, pressed);
5263
5264
5265
5266 if (!retValue && condition == WHEN_ANCESTOR_OF_FOCUSED_COMPONENT &&
5267 isFocusOwner() &&
5268 !Boolean.FALSE.equals(getClientProperty("JTable.autoStartsEdit"))) {
5269
5270 Component editorComponent = getEditorComponent();
5271 if (editorComponent == null) {
5272
5273 if (e == null || e.getID() != KeyEvent.KEY_PRESSED) {
5274 return false;
5275 }
5276
5277 int code = e.getKeyCode();
5278 if (code == KeyEvent.VK_SHIFT || code == KeyEvent.VK_CONTROL ||
5279 code == KeyEvent.VK_ALT) {
5280 return false;
5281 }
5282
5283 int leadRow = getSelectionModel().getLeadSelectionIndex();
5284 int leadColumn = getColumnModel().getSelectionModel().
5285 getLeadSelectionIndex();
5286 if (leadRow != -1 && leadColumn != -1 && !isEditing()) {
5287 if (!editCellAt(leadRow, leadColumn, e)) {
5288 return false;
5289 }
5290 }
5291 editorComponent = getEditorComponent();
5292 if (editorComponent == null) {
5293 return false;
5294 }
5295 }
5296
5297 if (editorComponent instanceof JComponent) {
5298 retValue = ((JComponent)editorComponent).processKeyBinding
5299 (ks, e, WHEN_FOCUSED, pressed);
5300
5301
5302
5303 if (getSurrendersFocusOnKeystroke()) {
5304 editorComponent.requestFocus();
5305 }
5306 }
5307 }
5308 return retValue;
5309 }
5310
5311 private void setLazyValue(Hashtable h, Class c, String s) {
5312 h.put(c, new SwingLazyValue(s));
5313 }
5314
5315 private void setLazyRenderer(Class c, String s) {
5316 setLazyValue(defaultRenderersByColumnClass, c, s);
5317 }
5318
5319
5320
5321
5322
5323
5324
5325 protected void createDefaultRenderers() {
5326 defaultRenderersByColumnClass = new UIDefaults(8, 0.75f);
5327
5328
5329 setLazyRenderer(Object.class, "javax.swing.table.DefaultTableCellRenderer$UIResource");
5330
5331
5332 setLazyRenderer(Number.class, "javax.swing.JTable$NumberRenderer");
5333
5334
5335 setLazyRenderer(Float.class, "javax.swing.JTable$DoubleRenderer");
5336 setLazyRenderer(Double.class, "javax.swing.JTable$DoubleRenderer");
5337
5338
5339 setLazyRenderer(Date.class, "javax.swing.JTable$DateRenderer");
5340
5341
5342 setLazyRenderer(Icon.class, "javax.swing.JTable$IconRenderer");
5343 setLazyRenderer(ImageIcon.class, "javax.swing.JTable$IconRenderer");
5344
5345
5346 setLazyRenderer(Boolean.class, "javax.swing.JTable$BooleanRenderer");
5347 }
5348
5349
5350
5351
5352 static class NumberRenderer extends DefaultTableCellRenderer.UIResource {
5353 public NumberRenderer() {
5354 super();
5355 setHorizontalAlignment(JLabel.RIGHT);
5356 }
5357 }
5358
5359 static class DoubleRenderer extends NumberRenderer {
5360 NumberFormat formatter;
5361 public DoubleRenderer() { super(); }
5362
5363 public void setValue(Object value) {
5364 if (formatter == null) {
5365 formatter = NumberFormat.getInstance();
5366 }
5367 setText((value == null) ? "" : formatter.format(value));
5368 }
5369 }
5370
5371 static class DateRenderer extends DefaultTableCellRenderer.UIResource {
5372 DateFormat formatter;
5373 public DateRenderer() { super(); }
5374
5375 public void setValue(Object value) {
5376 if (formatter==null) {
5377 formatter = DateFormat.getDateInstance();
5378 }
5379 setText((value == null) ? "" : formatter.format(value));
5380 }
5381 }
5382
5383 static class IconRenderer extends DefaultTableCellRenderer.UIResource {
5384 public IconRenderer() {
5385 super();
5386 setHorizontalAlignment(JLabel.CENTER);
5387 }
5388 public void setValue(Object value) { setIcon((value instanceof Icon) ? (Icon)value : null); }
5389 }
5390
5391
5392 static class BooleanRenderer extends JCheckBox implements TableCellRenderer, UIResource
5393 {
5394 private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
5395
5396 public BooleanRenderer() {
5397 super();
5398 setHorizontalAlignment(JLabel.CENTER);
5399 setBorderPainted(true);
5400 }
5401
5402 public Component getTableCellRendererComponent(JTable table, Object value,
5403 boolean isSelected, boolean hasFocus, int row, int column) {
5404 if (isSelected) {
5405 setForeground(table.getSelectionForeground());
5406 super.setBackground(table.getSelectionBackground());
5407 }
5408 else {
5409 setForeground(table.getForeground());
5410 setBackground(table.getBackground());
5411 }
5412 setSelected((value != null && ((Boolean)value).booleanValue()));
5413
5414 if (hasFocus) {
5415 setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
5416 } else {
5417 setBorder(noFocusBorder);
5418 }
5419
5420 return this;
5421 }
5422 }
5423
5424 private void setLazyEditor(Class c, String s) {
5425 setLazyValue(defaultEditorsByColumnClass, c, s);
5426 }
5427
5428
5429
5430
5431
5432 protected void createDefaultEditors() {
5433 defaultEditorsByColumnClass = new UIDefaults(3, 0.75f);
5434
5435
5436 setLazyEditor(Object.class, "javax.swing.JTable$GenericEditor");
5437
5438
5439 setLazyEditor(Number.class, "javax.swing.JTable$NumberEditor");
5440
5441
5442 setLazyEditor(Boolean.class, "javax.swing.JTable$BooleanEditor");
5443 }
5444
5445
5446
5447
5448 static class GenericEditor extends DefaultCellEditor {
5449
5450 Class[] argTypes = new Class[]{String.class};
5451 java.lang.reflect.Constructor constructor;
5452 Object value;
5453
5454 public GenericEditor() {
5455 super(new JTextField());
5456 getComponent().setName("Table.editor");
5457 }
5458
5459 public boolean stopCellEditing() {
5460 String s = (String)super.getCellEditorValue();
5461
5462
5463
5464
5465
5466
5467 if ("".equals(s)) {
5468 if (constructor.getDeclaringClass() == String.class) {
5469 value = s;
5470 }
5471 super.stopCellEditing();
5472 }
5473
5474 try {
5475 value = constructor.newInstance(new Object[]{s});
5476 }
5477 catch (Exception e) {
5478 ((JComponent)getComponent()).setBorder(new LineBorder(Color.red));
5479 return false;
5480 }
5481 return super.stopCellEditing();
5482 }
5483
5484 public Component getTableCellEditorComponent(JTable table, Object value,
5485 boolean isSelected,
5486 int row, int column) {
5487 this.value = null;
5488 ((JComponent)getComponent()).setBorder(new LineBorder(Color.black));
5489 try {
5490 Class<?> type = table.getColumnClass(column);
5491
5492
5493
5494
5495 if (type == Object.class) {
5496 type = String.class;
5497 }
5498 constructor = type.getConstructor(argTypes);
5499 }
5500 catch (Exception e) {
5501 return null;
5502 }
5503 return super.getTableCellEditorComponent(table, value, isSelected, row, column);
5504 }
5505
5506 public Object getCellEditorValue() {
5507 return value;
5508 }
5509 }
5510
5511 static class NumberEditor extends GenericEditor {
5512
5513 public NumberEditor() {
5514 ((JTextField)getComponent()).setHorizontalAlignment(JTextField.RIGHT);
5515 }
5516 }
5517
5518 static class BooleanEditor extends DefaultCellEditor {
5519 public BooleanEditor() {
5520 super(new JCheckBox());
5521 JCheckBox checkBox = (JCheckBox)getComponent();
5522 checkBox.setHorizontalAlignment(JCheckBox.CENTER);
5523 }
5524 }
5525
5526
5527
5528
5529 protected void initializeLocalVars() {
5530 updateSelectionOnSort = true;
5531 setOpaque(true);
5532 createDefaultRenderers();
5533 createDefaultEditors();
5534
5535 setTableHeader(createDefaultTableHeader());
5536
5537 setShowGrid(true);
5538 setAutoResizeMode(AUTO_RESIZE_SUBSEQUENT_COLUMNS);
5539 setRowHeight(16);
5540 isRowHeightSet = false;
5541 setRowMargin(1);
5542 setRowSelectionAllowed(true);
5543 setCellEditor(null);
5544 setEditingColumn(-1);
5545 setEditingRow(-1);
5546 setSurrendersFocusOnKeystroke(false);
5547 setPreferredScrollableViewportSize(new Dimension(450, 400));
5548
5549
5550 ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
5551 toolTipManager.registerComponent(this);
5552
5553 setAutoscrolls(true);
5554 }
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564 protected TableModel createDefaultDataModel() {
5565 return new DefaultTableModel();
5566 }
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576 protected TableColumnModel createDefaultColumnModel() {
5577 return new DefaultTableColumnModel();
5578 }
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588 protected ListSelectionModel createDefaultSelectionModel() {
5589 return new DefaultListSelectionModel();
5590 }
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600 protected JTableHeader createDefaultTableHeader() {
5601 return new JTableHeader(columnModel);
5602 }
5603
5604
5605
5606
5607 protected void resizeAndRepaint() {
5608 revalidate();
5609 repaint();
5610 }
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621 public TableCellEditor getCellEditor() {
5622 return cellEditor;
5623 }
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634 public void setCellEditor(TableCellEditor anEditor) {
5635 TableCellEditor oldEditor = cellEditor;
5636 cellEditor = anEditor;
5637 firePropertyChange("tableCellEditor", oldEditor, anEditor);
5638 }
5639
5640
5641
5642
5643
5644
5645
5646 public void setEditingColumn(int aColumn) {
5647 editingColumn = aColumn;
5648 }
5649
5650
5651
5652
5653
5654
5655
5656 public void setEditingRow(int aRow) {
5657 editingRow = aRow;
5658 }
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682 public TableCellRenderer getCellRenderer(int row, int column) {
5683 TableColumn tableColumn = getColumnModel().getColumn(column);
5684 TableCellRenderer renderer = tableColumn.getCellRenderer();
5685 if (renderer == null) {
5686 renderer = getDefaultRenderer(getColumnClass(column));
5687 }
5688 return renderer;
5689 }
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717 public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
5718 Object value = getValueAt(row, column);
5719
5720 boolean isSelected = false;
5721 boolean hasFocus = false;
5722
5723
5724 if (!isPaintingForPrint()) {
5725 isSelected = isCellSelected(row, column);
5726
5727 boolean rowIsLead =
5728 (selectionModel.getLeadSelectionIndex() == row);
5729 boolean colIsLead =
5730 (columnModel.getSelectionModel().getLeadSelectionIndex() == column);
5731
5732 hasFocus = (rowIsLead && colIsLead) && isFocusOwner();
5733 }
5734
5735 return renderer.getTableCellRendererComponent(this, value,
5736 isSelected, hasFocus,
5737 row, column);
5738 }
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761 public TableCellEditor getCellEditor(int row, int column) {
5762 TableColumn tableColumn = getColumnModel().getColumn(column);
5763 TableCellEditor editor = tableColumn.getCellEditor();
5764 if (editor == null) {
5765 editor = getDefaultEditor(getColumnClass(column));
5766 }
5767 return editor;
5768 }
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787 public Component prepareEditor(TableCellEditor editor, int row, int column) {
5788 Object value = getValueAt(row, column);
5789 boolean isSelected = isCellSelected(row, column);
5790 Component comp = editor.getTableCellEditorComponent(this, value, isSelected,
5791 row, column);
5792 if (comp instanceof JComponent) {
5793 JComponent jComp = (JComponent)comp;
5794 if (jComp.getNextFocusableComponent() == null) {
5795 jComp.setNextFocusableComponent(this);
5796 }
5797 }
5798 return comp;
5799 }
5800
5801
5802
5803
5804
5805 public void removeEditor() {
5806 KeyboardFocusManager.getCurrentKeyboardFocusManager().
5807 removePropertyChangeListener("permanentFocusOwner", editorRemover);
5808 editorRemover = null;
5809
5810 TableCellEditor editor = getCellEditor();
5811 if(editor != null) {
5812 editor.removeCellEditorListener(this);
5813 if (editorComp != null) {
5814 Component focusOwner =
5815 KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
5816 boolean isFocusOwnerInTheTable = focusOwner != null?
5817 SwingUtilities.isDescendingFrom(focusOwner, this):false;
5818 remove(editorComp);
5819 if(isFocusOwnerInTheTable) {
5820 requestFocusInWindow();
5821 }
5822 }
5823
5824 Rectangle cellRect = getCellRect(editingRow, editingColumn, false);
5825
5826 setCellEditor(null);
5827 setEditingColumn(-1);
5828 setEditingRow(-1);
5829 editorComp = null;
5830
5831 repaint(cellRect);
5832 }
5833 }
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843 private void writeObject(ObjectOutputStream s) throws IOException {
5844 s.defaultWriteObject();
5845 if (getUIClassID().equals(uiClassID)) {
5846 byte count = JComponent.getWriteObjCounter(this);
5847 JComponent.setWriteObjCounter(this, --count);
5848 if (count == 0 && ui != null) {
5849 ui.installUI(this);
5850 }
5851 }
5852 }
5853
5854 private void readObject(ObjectInputStream s)
5855 throws IOException, ClassNotFoundException
5856 {
5857 s.defaultReadObject();
5858 if ((ui != null) && (getUIClassID().equals(uiClassID))) {
5859 ui.installUI(this);
5860 }
5861 createDefaultRenderers();
5862 createDefaultEditors();
5863
5864
5865
5866
5867 if (getToolTipText() == null) {
5868 ToolTipManager.sharedInstance().registerComponent(this);
5869 }
5870 }
5871
5872
5873
5874
5875 void compWriteObjectNotify() {
5876 super.compWriteObjectNotify();
5877
5878
5879 if (getToolTipText() == null) {
5880 ToolTipManager.sharedInstance().unregisterComponent(this);
5881 }
5882 }
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893 protected String paramString() {
5894 String gridColorString = (gridColor != null ?
5895 gridColor.toString() : "");
5896 String showHorizontalLinesString = (showHorizontalLines ?
5897 "true" : "false");
5898 String showVerticalLinesString = (showVerticalLines ?
5899 "true" : "false");
5900 String autoResizeModeString;
5901 if (autoResizeMode == AUTO_RESIZE_OFF) {
5902 autoResizeModeString = "AUTO_RESIZE_OFF";
5903 } else if (autoResizeMode == AUTO_RESIZE_NEXT_COLUMN) {
5904 autoResizeModeString = "AUTO_RESIZE_NEXT_COLUMN";
5905 } else if (autoResizeMode == AUTO_RESIZE_SUBSEQUENT_COLUMNS) {
5906 autoResizeModeString = "AUTO_RESIZE_SUBSEQUENT_COLUMNS";
5907 } else if (autoResizeMode == AUTO_RESIZE_LAST_COLUMN) {
5908 autoResizeModeString = "AUTO_RESIZE_LAST_COLUMN";
5909 } else if (autoResizeMode == AUTO_RESIZE_ALL_COLUMNS) {
5910 autoResizeModeString = "AUTO_RESIZE_ALL_COLUMNS";
5911 } else autoResizeModeString = "";
5912 String autoCreateColumnsFromModelString = (autoCreateColumnsFromModel ?
5913 "true" : "false");
5914 String preferredViewportSizeString = (preferredViewportSize != null ?
5915 preferredViewportSize.toString()
5916 : "");
5917 String rowSelectionAllowedString = (rowSelectionAllowed ?
5918 "true" : "false");
5919 String cellSelectionEnabledString = (cellSelectionEnabled ?
5920 "true" : "false");
5921 String selectionForegroundString = (selectionForeground != null ?
5922 selectionForeground.toString() :
5923 "");
5924 String selectionBackgroundString = (selectionBackground != null ?
5925 selectionBackground.toString() :
5926 "");
5927
5928 return super.paramString() +
5929 ",autoCreateColumnsFromModel=" + autoCreateColumnsFromModelString +
5930 ",autoResizeMode=" + autoResizeModeString +
5931 ",cellSelectionEnabled=" + cellSelectionEnabledString +
5932 ",editingColumn=" + editingColumn +
5933 ",editingRow=" + editingRow +
5934 ",gridColor=" + gridColorString +
5935 ",preferredViewportSize=" + preferredViewportSizeString +
5936 ",rowHeight=" + rowHeight +
5937 ",rowMargin=" + rowMargin +
5938 ",rowSelectionAllowed=" + rowSelectionAllowedString +
5939 ",selectionBackground=" + selectionBackgroundString +
5940 ",selectionForeground=" + selectionForegroundString +
5941 ",showHorizontalLines=" + showHorizontalLinesString +
5942 ",showVerticalLines=" + showVerticalLinesString;
5943 }
5944
5945
5946
5947
5948
5949 class CellEditorRemover implements PropertyChangeListener {
5950 KeyboardFocusManager focusManager;
5951
5952 public CellEditorRemover(KeyboardFocusManager fm) {
5953 this.focusManager = fm;
5954 }
5955
5956 public void propertyChange(PropertyChangeEvent ev) {
5957 if (!isEditing() || getClientProperty("terminateEditOnFocusLost") != Boolean.TRUE) {
5958 return;
5959 }
5960
5961 Component c = focusManager.getPermanentFocusOwner();
5962 while (c != null) {
5963 if (c == JTable.this) {
5964
5965 return;
5966 } else if ((c instanceof Window) ||
5967 (c instanceof Applet && c.getParent() == null)) {
5968 if (c == SwingUtilities.getRoot(JTable.this)) {
5969 if (!getCellEditor().stopCellEditing()) {
5970 getCellEditor().cancelCellEditing();
5971 }
5972 }
5973 break;
5974 }
5975 c = c.getParent();
5976 }
5977 }
5978 }
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004 public boolean print() throws PrinterException {
6005
6006 return print(PrintMode.FIT_WIDTH);
6007 }
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030 public boolean print(PrintMode printMode) throws PrinterException {
6031
6032 return print(printMode, null, null);
6033 }
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062 public boolean print(PrintMode printMode,
6063 MessageFormat headerFormat,
6064 MessageFormat footerFormat) throws PrinterException {
6065
6066 boolean showDialogs = !GraphicsEnvironment.isHeadless();
6067 return print(printMode, headerFormat, footerFormat,
6068 showDialogs, null, showDialogs);
6069 }
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104 public boolean print(PrintMode printMode,
6105 MessageFormat headerFormat,
6106 MessageFormat footerFormat,
6107 boolean showPrintDialog,
6108 PrintRequestAttributeSet attr,
6109 boolean interactive) throws PrinterException,
6110 HeadlessException {
6111
6112 return print(printMode,
6113 headerFormat,
6114 footerFormat,
6115 showPrintDialog,
6116 attr,
6117 interactive,
6118 null);
6119 }
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199 public boolean print(PrintMode printMode,
6200 MessageFormat headerFormat,
6201 MessageFormat footerFormat,
6202 boolean showPrintDialog,
6203 PrintRequestAttributeSet attr,
6204 boolean interactive,
6205 PrintService service) throws PrinterException,
6206 HeadlessException {
6207
6208
6209 boolean isHeadless = GraphicsEnvironment.isHeadless();
6210 if (isHeadless) {
6211 if (showPrintDialog) {
6212 throw new HeadlessException("Can't show print dialog.");
6213 }
6214
6215 if (interactive) {
6216 throw new HeadlessException("Can't run interactively.");
6217 }
6218 }
6219
6220
6221
6222
6223 final PrinterJob job = PrinterJob.getPrinterJob();
6224
6225 if (isEditing()) {
6226
6227 if (!getCellEditor().stopCellEditing()) {
6228 getCellEditor().cancelCellEditing();
6229 }
6230 }
6231
6232 if (attr == null) {
6233 attr = new HashPrintRequestAttributeSet();
6234 }
6235
6236 final PrintingStatus printingStatus;
6237
6238
6239 Printable printable =
6240 getPrintable(printMode, headerFormat, footerFormat);
6241
6242 if (interactive) {
6243
6244 printable = new ThreadSafePrintable(printable);
6245 printingStatus = PrintingStatus.createPrintingStatus(this, job);
6246 printable = printingStatus.createNotificationPrintable(printable);
6247 } else {
6248
6249 printingStatus = null;
6250 }
6251
6252
6253 job.setPrintable(printable);
6254
6255
6256 if (service != null) {
6257 job.setPrintService(service);
6258 }
6259
6260
6261 if (showPrintDialog && !job.printDialog(attr)) {
6262
6263 return false;
6264 }
6265
6266
6267 if (!interactive) {
6268
6269 job.print(attr);
6270
6271
6272 return true;
6273 }
6274
6275
6276 printError = null;
6277
6278
6279 final Object lock = new Object();
6280
6281
6282 final PrintRequestAttributeSet copyAttr = attr;
6283
6284
6285
6286 Runnable runnable = new Runnable() {
6287 public void run() {
6288 try {
6289
6290 job.print(copyAttr);
6291 } catch (Throwable t) {
6292
6293 synchronized(lock) {
6294 printError = t;
6295 }
6296 } finally {
6297
6298 printingStatus.dispose();
6299 }
6300 }
6301 };
6302
6303
6304 Thread th = new Thread(runnable);
6305 th.start();
6306
6307 printingStatus.showModal(true);
6308
6309
6310 Throwable pe;
6311 synchronized(lock) {
6312 pe = printError;
6313 printError = null;
6314 }
6315
6316
6317 if (pe != null) {
6318
6319
6320 if (pe instanceof PrinterAbortException) {
6321 return false;
6322 } else if (pe instanceof PrinterException) {
6323 throw (PrinterException)pe;
6324 } else if (pe instanceof RuntimeException) {
6325 throw (RuntimeException)pe;
6326 } else if (pe instanceof Error) {
6327 throw (Error)pe;
6328 }
6329
6330
6331 throw new AssertionError(pe);
6332 }
6333
6334 return true;
6335 }
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438 public Printable getPrintable(PrintMode printMode,
6439 MessageFormat headerFormat,
6440 MessageFormat footerFormat) {
6441
6442 return new TablePrintable(this, printMode, headerFormat, footerFormat);
6443 }
6444
6445
6446
6447
6448
6449
6450 private class ThreadSafePrintable implements Printable {
6451
6452
6453 private Printable printDelegate;
6454
6455
6456
6457
6458 private int retVal;
6459
6460
6461
6462
6463 private Throwable retThrowable;
6464
6465
6466
6467
6468
6469
6470
6471 public ThreadSafePrintable(Printable printDelegate) {
6472 this.printDelegate = printDelegate;
6473 }
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489 public int print(final Graphics graphics,
6490 final PageFormat pageFormat,
6491 final int pageIndex) throws PrinterException {
6492
6493
6494 Runnable runnable = new Runnable() {
6495 public synchronized void run() {
6496 try {
6497
6498 retVal = printDelegate.print(graphics, pageFormat, pageIndex);
6499 } catch (Throwable throwable) {
6500
6501 retThrowable = throwable;
6502 } finally {
6503
6504 notifyAll();
6505 }
6506 }
6507 };
6508
6509 synchronized(runnable) {
6510
6511 retVal = -1;
6512 retThrowable = null;
6513
6514
6515 SwingUtilities.invokeLater(runnable);
6516
6517
6518 while (retVal == -1 && retThrowable == null) {
6519 try {
6520 runnable.wait();
6521 } catch (InterruptedException ie) {
6522
6523 }
6524 }
6525
6526
6527 if (retThrowable != null) {
6528 if (retThrowable instanceof PrinterException) {
6529 throw (PrinterException)retThrowable;
6530 } else if (retThrowable instanceof RuntimeException) {
6531 throw (RuntimeException)retThrowable;
6532 } else if (retThrowable instanceof Error) {
6533 throw (Error)retThrowable;
6534 }
6535
6536
6537 throw new AssertionError(retThrowable);
6538 }
6539
6540 return retVal;
6541 }
6542 }
6543 }
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559 public AccessibleContext getAccessibleContext() {
6560 if (accessibleContext == null) {
6561 accessibleContext = new AccessibleJTable();
6562 }
6563 return accessibleContext;
6564 }
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584 protected class AccessibleJTable extends AccessibleJComponent
6585 implements AccessibleSelection, ListSelectionListener, TableModelListener,
6586 TableColumnModelListener, CellEditorListener, PropertyChangeListener,
6587 AccessibleExtendedTable {
6588
6589 int lastSelectedRow;
6590 int lastSelectedCol;
6591
6592
6593
6594
6595
6596
6597 protected AccessibleJTable() {
6598 super();
6599 JTable.this.addPropertyChangeListener(this);
6600 JTable.this.getSelectionModel().addListSelectionListener(this);
6601 TableColumnModel tcm = JTable.this.getColumnModel();
6602 tcm.addColumnModelListener(this);
6603 tcm.getSelectionModel().addListSelectionListener(this);
6604 JTable.this.getModel().addTableModelListener(this);
6605 lastSelectedRow = JTable.this.getSelectedRow();
6606 lastSelectedCol = JTable.this.getSelectedColumn();
6607 }
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617 public void propertyChange(PropertyChangeEvent e) {
6618 String name = e.getPropertyName();
6619 Object oldValue = e.getOldValue();
6620 Object newValue = e.getNewValue();
6621
6622
6623 if (name.compareTo("model") == 0) {
6624
6625 if (oldValue != null && oldValue instanceof TableModel) {
6626 ((TableModel) oldValue).removeTableModelListener(this);
6627 }
6628 if (newValue != null && newValue instanceof TableModel) {
6629 ((TableModel) newValue).addTableModelListener(this);
6630 }
6631
6632
6633 } else if (name.compareTo("selectionModel") == 0) {
6634
6635 Object source = e.getSource();
6636 if (source == JTable.this) {
6637
6638 if (oldValue != null &&
6639 oldValue instanceof ListSelectionModel) {
6640 ((ListSelectionModel) oldValue).removeListSelectionListener(this);
6641 }
6642 if (newValue != null &&
6643 newValue instanceof ListSelectionModel) {
6644 ((ListSelectionModel) newValue).addListSelectionListener(this);
6645 }
6646
6647 } else if (source == JTable.this.getColumnModel()) {
6648
6649 if (oldValue != null &&
6650 oldValue instanceof ListSelectionModel) {
6651 ((ListSelectionModel) oldValue).removeListSelectionListener(this);
6652 }
6653 if (newValue != null &&
6654 newValue instanceof ListSelectionModel) {
6655 ((ListSelectionModel) newValue).addListSelectionListener(this);
6656 }
6657
6658 } else {
6659
6660 }
6661
6662
6663
6664 } else if (name.compareTo("columnModel") == 0) {
6665
6666 if (oldValue != null && oldValue instanceof TableColumnModel) {
6667 TableColumnModel tcm = (TableColumnModel) oldValue;
6668 tcm.removeColumnModelListener(this);
6669 tcm.getSelectionModel().removeListSelectionListener(this);
6670 }
6671 if (newValue != null && newValue instanceof TableColumnModel) {
6672 TableColumnModel tcm = (TableColumnModel) newValue;
6673 tcm.addColumnModelListener(this);
6674 tcm.getSelectionModel().addListSelectionListener(this);
6675 }
6676
6677
6678 } else if (name.compareTo("tableCellEditor") == 0) {
6679
6680 if (oldValue != null && oldValue instanceof TableCellEditor) {
6681 ((TableCellEditor) oldValue).removeCellEditorListener(this);
6682 }
6683 if (newValue != null && newValue instanceof TableCellEditor) {
6684 ((TableCellEditor) newValue).addCellEditorListener(this);
6685 }
6686 }
6687 }
6688
6689
6690
6691
6692
6693
6694
6695 protected class AccessibleJTableModelChange
6696 implements AccessibleTableModelChange {
6697
6698 protected int type;
6699 protected int firstRow;
6700 protected int lastRow;
6701 protected int firstColumn;
6702 protected int lastColumn;
6703
6704 protected AccessibleJTableModelChange(int type, int firstRow,
6705 int lastRow, int firstColumn,
6706 int lastColumn) {
6707 this.type = type;
6708 this.firstRow = firstRow;
6709 this.lastRow = lastRow;
6710 this.firstColumn = firstColumn;
6711 this.lastColumn = lastColumn;
6712 }
6713
6714 public int getType() {
6715 return type;
6716 }
6717
6718 public int getFirstRow() {
6719 return firstRow;
6720 }
6721
6722 public int getLastRow() {
6723 return lastRow;
6724 }
6725
6726 public int getFirstColumn() {
6727 return firstColumn;
6728 }
6729
6730 public int getLastColumn() {
6731 return lastColumn;
6732 }
6733 }
6734
6735
6736
6737
6738 public void tableChanged(TableModelEvent e) {
6739 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
6740 null, null);
6741 if (e != null) {
6742 int firstColumn = e.getColumn();
6743 int lastColumn = e.getColumn();
6744 if (firstColumn == TableModelEvent.ALL_COLUMNS) {
6745 firstColumn = 0;
6746 lastColumn = getColumnCount() - 1;
6747 }
6748
6749
6750
6751 AccessibleJTableModelChange change =
6752 new AccessibleJTableModelChange(e.getType(),
6753 e.getFirstRow(),
6754 e.getLastRow(),
6755 firstColumn,
6756 lastColumn);
6757 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
6758 null, change);
6759 }
6760 }
6761
6762
6763
6764
6765 public void tableRowsInserted(TableModelEvent e) {
6766 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
6767 null, null);
6768
6769
6770
6771 int firstColumn = e.getColumn();
6772 int lastColumn = e.getColumn();
6773 if (firstColumn == TableModelEvent.ALL_COLUMNS) {
6774 firstColumn = 0;
6775 lastColumn = getColumnCount() - 1;
6776 }
6777 AccessibleJTableModelChange change =
6778 new AccessibleJTableModelChange(e.getType(),
6779 e.getFirstRow(),
6780 e.getLastRow(),
6781 firstColumn,
6782 lastColumn);
6783 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
6784 null, change);
6785 }
6786
6787
6788
6789
6790 public void tableRowsDeleted(TableModelEvent e) {
6791 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
6792 null, null);
6793
6794
6795
6796 int firstColumn = e.getColumn();
6797 int lastColumn = e.getColumn();
6798 if (firstColumn == TableModelEvent.ALL_COLUMNS) {
6799 firstColumn = 0;
6800 lastColumn = getColumnCount() - 1;
6801 }
6802 AccessibleJTableModelChange change =
6803 new AccessibleJTableModelChange(e.getType(),
6804 e.getFirstRow(),
6805 e.getLastRow(),
6806 firstColumn,
6807 lastColumn);
6808 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
6809 null, change);
6810 }
6811
6812
6813
6814
6815 public void columnAdded(TableColumnModelEvent e) {
6816 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
6817 null, null);
6818
6819
6820
6821 int type = AccessibleTableModelChange.INSERT;
6822 AccessibleJTableModelChange change =
6823 new AccessibleJTableModelChange(type,
6824 0,
6825 0,
6826 e.getFromIndex(),
6827 e.getToIndex());
6828 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
6829 null, change);
6830 }
6831
6832
6833
6834
6835 public void columnRemoved(TableColumnModelEvent e) {
6836 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
6837 null, null);
6838
6839
6840 int type = AccessibleTableModelChange.DELETE;
6841 AccessibleJTableModelChange change =
6842 new AccessibleJTableModelChange(type,
6843 0,
6844 0,
6845 e.getFromIndex(),
6846 e.getToIndex());
6847 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
6848 null, change);
6849 }
6850
6851
6852
6853
6854
6855
6856 public void columnMoved(TableColumnModelEvent e) {
6857 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
6858 null, null);
6859
6860
6861
6862 int type = AccessibleTableModelChange.DELETE;
6863 AccessibleJTableModelChange change =
6864 new AccessibleJTableModelChange(type,
6865 0,
6866 0,
6867 e.getFromIndex(),
6868 e.getFromIndex());
6869 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
6870 null, change);
6871
6872 int type2 = AccessibleTableModelChange.INSERT;
6873 AccessibleJTableModelChange change2 =
6874 new AccessibleJTableModelChange(type2,
6875 0,
6876 0,
6877 e.getToIndex(),
6878 e.getToIndex());
6879 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
6880 null, change2);
6881 }
6882
6883
6884
6885
6886
6887
6888 public void columnMarginChanged(ChangeEvent e) {
6889 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
6890 null, null);
6891 }
6892
6893
6894
6895
6896
6897
6898 public void columnSelectionChanged(ListSelectionEvent e) {
6899
6900 }
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910 public void editingStopped(ChangeEvent e) {
6911
6912
6913 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
6914 null, null);
6915 }
6916
6917
6918
6919
6920
6921
6922
6923 public void editingCanceled(ChangeEvent e) {
6924
6925 }
6926
6927
6928
6929
6930 public void valueChanged(ListSelectionEvent e) {
6931 firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
6932 Boolean.valueOf(false), Boolean.valueOf(true));
6933
6934 int selectedRow = JTable.this.getSelectedRow();
6935 int selectedCol = JTable.this.getSelectedColumn();
6936 if (selectedRow != lastSelectedRow ||
6937 selectedCol != lastSelectedCol) {
6938 Accessible oldA = getAccessibleAt(lastSelectedRow,
6939 lastSelectedCol);
6940 Accessible newA = getAccessibleAt(selectedRow, selectedCol);
6941 firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
6942 oldA, newA);
6943 lastSelectedRow = selectedRow;
6944 lastSelectedCol = selectedCol;
6945 }
6946 }
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961 public AccessibleSelection getAccessibleSelection() {
6962 return this;
6963 }
6964
6965
6966
6967
6968
6969
6970
6971
6972 public AccessibleRole getAccessibleRole() {
6973 return AccessibleRole.TABLE;
6974 }
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986 public Accessible getAccessibleAt(Point p) {
6987 int column = columnAtPoint(p);
6988 int row = rowAtPoint(p);
6989
6990 if ((column != -1) && (row != -1)) {
6991 TableColumn aColumn = getColumnModel().getColumn(column);
6992 TableCellRenderer renderer = aColumn.getCellRenderer();
6993 if (renderer == null) {
6994 Class<?> columnClass = getColumnClass(column);
6995 renderer = getDefaultRenderer(columnClass);
6996 }
6997 Component component = renderer.getTableCellRendererComponent(
6998 JTable.this, null, false, false,
6999 row, column);
7000 return new AccessibleJTableCell(JTable.this, row, column,
7001 getAccessibleIndexAt(row, column));
7002 }
7003 return null;
7004 }
7005
7006
7007
7008
7009
7010
7011
7012
7013 public int getAccessibleChildrenCount() {
7014 return (JTable.this.getColumnCount() * JTable.this.getRowCount());
7015 }
7016
7017
7018
7019
7020
7021
7022
7023 public Accessible getAccessibleChild(int i) {
7024 if (i < 0 || i >= getAccessibleChildrenCount()) {
7025 return null;
7026 } else {
7027
7028
7029 int column = getAccessibleColumnAtIndex(i);
7030 int row = getAccessibleRowAtIndex(i);
7031
7032 TableColumn aColumn = getColumnModel().getColumn(column);
7033 TableCellRenderer renderer = aColumn.getCellRenderer();
7034 if (renderer == null) {
7035 Class<?> columnClass = getColumnClass(column);
7036 renderer = getDefaultRenderer(columnClass);
7037 }
7038 Component component = renderer.getTableCellRendererComponent(
7039 JTable.this, null, false, false,
7040 row, column);
7041 return new AccessibleJTableCell(JTable.this, row, column,
7042 getAccessibleIndexAt(row, column));
7043 }
7044 }
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055 public int getAccessibleSelectionCount() {
7056 int rowsSel = JTable.this.getSelectedRowCount();
7057 int colsSel = JTable.this.getSelectedColumnCount();
7058
7059 if (JTable.this.cellSelectionEnabled) {
7060 return rowsSel * colsSel;
7061
7062 } else {
7063
7064 if (JTable.this.getRowSelectionAllowed() &&
7065 JTable.this.getColumnSelectionAllowed()) {
7066 return rowsSel * JTable.this.getColumnCount() +
7067 colsSel * JTable.this.getRowCount() -
7068 rowsSel * colsSel;
7069
7070
7071 } else if (JTable.this.getRowSelectionAllowed()) {
7072 return rowsSel * JTable.this.getColumnCount();
7073
7074
7075 } else if (JTable.this.getColumnSelectionAllowed()) {
7076 return colsSel * JTable.this.getRowCount();
7077
7078 } else {
7079 return 0;
7080 }
7081 }
7082 }
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097 public Accessible getAccessibleSelection(int i) {
7098 if (i < 0 || i > getAccessibleSelectionCount()) {
7099 return null;
7100 }
7101
7102 int rowsSel = JTable.this.getSelectedRowCount();
7103 int colsSel = JTable.this.getSelectedColumnCount();
7104 int rowIndicies[] = getSelectedRows();
7105 int colIndicies[] = getSelectedColumns();
7106 int ttlCols = JTable.this.getColumnCount();
7107 int ttlRows = JTable.this.getRowCount();
7108 int r;
7109 int c;
7110
7111 if (JTable.this.cellSelectionEnabled) {
7112 r = rowIndicies[i / colsSel];
7113 c = colIndicies[i % colsSel];
7114 return getAccessibleChild((r * ttlCols) + c);
7115 } else {
7116
7117
7118 if (JTable.this.getRowSelectionAllowed() &&
7119 JTable.this.getColumnSelectionAllowed()) {
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142 int curIndex = i;
7143 final int IN_ROW = 0;
7144 final int NOT_IN_ROW = 1;
7145 int state = (rowIndicies[0] == 0 ? IN_ROW : NOT_IN_ROW);
7146 int j = 0;
7147 int prevRow = -1;
7148 while (j < rowIndicies.length) {
7149 switch (state) {
7150
7151 case IN_ROW:
7152 if (curIndex < ttlCols) {
7153 c = curIndex % ttlCols;
7154 r = rowIndicies[j];
7155 return getAccessibleChild((r * ttlCols) + c);
7156 } else {
7157 curIndex -= ttlCols;
7158 }
7159
7160 if (j + 1 == rowIndicies.length ||
7161 rowIndicies[j] != rowIndicies[j+1] - 1) {
7162 state = NOT_IN_ROW;
7163 prevRow = rowIndicies[j];
7164 }
7165 j++;
7166 break;
7167
7168 case NOT_IN_ROW:
7169 if (curIndex <
7170 (colsSel * (rowIndicies[j] -
7171 (prevRow == -1 ? 0 : (prevRow + 1))))) {
7172
7173
7174 c = colIndicies[curIndex % colsSel];
7175 r = (j > 0 ? rowIndicies[j-1] + 1 : 0)
7176 + curIndex / colsSel;
7177 return getAccessibleChild((r * ttlCols) + c);
7178 } else {
7179 curIndex -= colsSel * (rowIndicies[j] -
7180 (prevRow == -1 ? 0 : (prevRow + 1)));
7181 }
7182 state = IN_ROW;
7183 break;
7184 }
7185 }
7186
7187
7188 if (curIndex <
7189 (colsSel * (ttlRows -
7190 (prevRow == -1 ? 0 : (prevRow + 1))))) {
7191 c = colIndicies[curIndex % colsSel];
7192 r = rowIndicies[j-1] + curIndex / colsSel + 1;
7193 return getAccessibleChild((r * ttlCols) + c);
7194 } else {
7195
7196
7197 }
7198
7199
7200 } else if (JTable.this.getRowSelectionAllowed()) {
7201 c = i % ttlCols;
7202 r = rowIndicies[i / ttlCols];
7203 return getAccessibleChild((r * ttlCols) + c);
7204
7205
7206 } else if (JTable.this.getColumnSelectionAllowed()) {
7207 c = colIndicies[i % colsSel];
7208 r = i / colsSel;
7209 return getAccessibleChild((r * ttlCols) + c);
7210 }
7211 }
7212 return null;
7213 }
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223 public boolean isAccessibleChildSelected(int i) {
7224 int column = getAccessibleColumnAtIndex(i);
7225 int row = getAccessibleRowAtIndex(i);
7226 return JTable.this.isCellSelected(row, column);
7227 }
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243 public void addAccessibleSelection(int i) {
7244
7245 int column = getAccessibleColumnAtIndex(i);
7246 int row = getAccessibleRowAtIndex(i);
7247 JTable.this.changeSelection(row, column, true, false);
7248 }
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261 public void removeAccessibleSelection(int i) {
7262 if (JTable.this.cellSelectionEnabled) {
7263 int column = getAccessibleColumnAtIndex(i);
7264 int row = getAccessibleRowAtIndex(i);
7265 JTable.this.removeRowSelectionInterval(row, row);
7266 JTable.this.removeColumnSelectionInterval(column, column);
7267 }
7268 }
7269
7270
7271
7272
7273
7274 public void clearAccessibleSelection() {
7275 JTable.this.clearSelection();
7276 }
7277
7278
7279
7280
7281
7282
7283 public void selectAllAccessibleSelection() {
7284 if (JTable.this.cellSelectionEnabled) {
7285 JTable.this.selectAll();
7286 }
7287 }
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299 public int getAccessibleRow(int index) {
7300 return getAccessibleRowAtIndex(index);
7301 }
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311 public int getAccessibleColumn(int index) {
7312 return getAccessibleColumnAtIndex(index);
7313 }
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324 public int getAccessibleIndex(int r, int c) {
7325 return getAccessibleIndexAt(r, c);
7326 }
7327
7328
7329
7330
7331
7332 private Accessible caption;
7333 private Accessible summary;
7334 private Accessible [] rowDescription;
7335 private Accessible [] columnDescription;
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347 public AccessibleTable getAccessibleTable() {
7348 return this;
7349 }
7350
7351
7352
7353
7354
7355
7356
7357 public Accessible getAccessibleCaption() {
7358 return this.caption;
7359 }
7360
7361
7362
7363
7364
7365
7366
7367 public void setAccessibleCaption(Accessible a) {
7368 Accessible oldCaption = caption;
7369 this.caption = a;
7370 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_CAPTION_CHANGED,
7371 oldCaption, this.caption);
7372 }
7373
7374
7375
7376
7377
7378
7379
7380 public Accessible getAccessibleSummary() {
7381 return this.summary;
7382 }
7383
7384
7385
7386
7387
7388
7389
7390 public void setAccessibleSummary(Accessible a) {
7391 Accessible oldSummary = summary;
7392 this.summary = a;
7393 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_SUMMARY_CHANGED,
7394 oldSummary, this.summary);
7395 }
7396
7397
7398
7399
7400
7401
7402 public int getAccessibleRowCount() {
7403 return JTable.this.getRowCount();
7404 }
7405
7406
7407
7408
7409
7410
7411 public int getAccessibleColumnCount() {
7412 return JTable.this.getColumnCount();
7413 }
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424 public Accessible getAccessibleAt(int r, int c) {
7425 return getAccessibleChild((r * getAccessibleColumnCount()) + c);
7426 }
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436 public int getAccessibleRowExtentAt(int r, int c) {
7437 return 1;
7438 }
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448 public int getAccessibleColumnExtentAt(int r, int c) {
7449 return 1;
7450 }
7451
7452
7453
7454
7455
7456
7457
7458
7459 public AccessibleTable getAccessibleRowHeader() {
7460
7461 return null;
7462 }
7463
7464
7465
7466
7467
7468
7469
7470
7471 public void setAccessibleRowHeader(AccessibleTable a) {
7472
7473 }
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483 public AccessibleTable getAccessibleColumnHeader() {
7484 JTableHeader header = JTable.this.getTableHeader();
7485 return header == null ? null : new AccessibleTableHeader(header);
7486 }
7487
7488
7489
7490
7491 private class AccessibleTableHeader implements AccessibleTable {
7492 private JTableHeader header;
7493 private TableColumnModel headerModel;
7494
7495 AccessibleTableHeader(JTableHeader header) {
7496 this.header = header;
7497 this.headerModel = header.getColumnModel();
7498 }
7499
7500
7501
7502
7503
7504
7505 public Accessible getAccessibleCaption() { return null; }
7506
7507
7508
7509
7510
7511
7512
7513 public void setAccessibleCaption(Accessible a) {}
7514
7515
7516
7517
7518
7519
7520 public Accessible getAccessibleSummary() { return null; }
7521
7522
7523
7524
7525
7526
7527 public void setAccessibleSummary(Accessible a) {}
7528
7529
7530
7531
7532
7533
7534 public int getAccessibleRowCount() { return 1; }
7535
7536
7537
7538
7539
7540
7541 public int getAccessibleColumnCount() {
7542 return headerModel.getColumnCount();
7543 }
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553 public Accessible getAccessibleAt(int row, int column) {
7554
7555
7556
7557 TableColumn aColumn = headerModel.getColumn(column);
7558 TableCellRenderer renderer = aColumn.getHeaderRenderer();
7559 if (renderer == null) {
7560 renderer = header.getDefaultRenderer();
7561 }
7562 Component component = renderer.getTableCellRendererComponent(
7563 header.getTable(),
7564 aColumn.getHeaderValue(), false, false,
7565 -1, column);
7566
7567 return new AccessibleJTableHeaderCell(row, column,
7568 JTable.this.getTableHeader(),
7569 component);
7570 }
7571
7572
7573
7574
7575
7576
7577
7578
7579 public int getAccessibleRowExtentAt(int r, int c) { return 1; }
7580
7581
7582
7583
7584
7585
7586
7587
7588 public int getAccessibleColumnExtentAt(int r, int c) { return 1; }
7589
7590
7591
7592
7593
7594
7595
7596 public AccessibleTable getAccessibleRowHeader() { return null; }
7597
7598
7599
7600
7601
7602
7603
7604 public void setAccessibleRowHeader(AccessibleTable table) {}
7605
7606
7607
7608
7609
7610
7611
7612 public AccessibleTable getAccessibleColumnHeader() { return null; }
7613
7614
7615
7616
7617
7618
7619
7620
7621 public void setAccessibleColumnHeader(AccessibleTable table) {}
7622
7623
7624
7625
7626
7627
7628
7629
7630 public Accessible getAccessibleRowDescription(int r) { return null; }
7631
7632
7633
7634
7635
7636
7637
7638
7639 public void setAccessibleRowDescription(int r, Accessible a) {}
7640
7641
7642
7643
7644
7645
7646
7647
7648 public Accessible getAccessibleColumnDescription(int c) { return null; }
7649
7650
7651
7652
7653
7654
7655
7656
7657 public void setAccessibleColumnDescription(int c, Accessible a) {}
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670 public boolean isAccessibleSelected(int r, int c) { return false; }
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681 public boolean isAccessibleRowSelected(int r) { return false; }
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692 public boolean isAccessibleColumnSelected(int c) { return false; }
7693
7694
7695
7696
7697
7698
7699
7700
7701 public int [] getSelectedAccessibleRows() { return new int[0]; }
7702
7703
7704
7705
7706
7707
7708
7709
7710 public int [] getSelectedAccessibleColumns() { return new int[0]; }
7711 }
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721 public void setAccessibleColumnHeader(AccessibleTable a) {
7722
7723 }
7724
7725
7726
7727
7728
7729
7730
7731
7732 public Accessible getAccessibleRowDescription(int r) {
7733 if (r < 0 || r >= getAccessibleRowCount()) {
7734 throw new IllegalArgumentException(Integer.toString(r));
7735 }
7736 if (rowDescription == null) {
7737 return null;
7738 } else {
7739 return rowDescription[r];
7740 }
7741 }
7742
7743
7744
7745
7746
7747
7748
7749
7750 public void setAccessibleRowDescription(int r, Accessible a) {
7751 if (r < 0 || r >= getAccessibleRowCount()) {
7752 throw new IllegalArgumentException(Integer.toString(r));
7753 }
7754 if (rowDescription == null) {
7755 int numRows = getAccessibleRowCount();
7756 rowDescription = new Accessible[numRows];
7757 }
7758 rowDescription[r] = a;
7759 }
7760
7761
7762
7763
7764
7765
7766
7767
7768 public Accessible getAccessibleColumnDescription(int c) {
7769 if (c < 0 || c >= getAccessibleColumnCount()) {
7770 throw new IllegalArgumentException(Integer.toString(c));
7771 }
7772 if (columnDescription == null) {
7773 return null;
7774 } else {
7775 return columnDescription[c];
7776 }
7777 }
7778
7779
7780
7781
7782
7783
7784
7785
7786 public void setAccessibleColumnDescription(int c, Accessible a) {
7787 if (c < 0 || c >= getAccessibleColumnCount()) {
7788 throw new IllegalArgumentException(Integer.toString(c));
7789 }
7790 if (columnDescription == null) {
7791 int numColumns = getAccessibleColumnCount();
7792 columnDescription = new Accessible[numColumns];
7793 }
7794 columnDescription[c] = a;
7795 }
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807 public boolean isAccessibleSelected(int r, int c) {
7808 return JTable.this.isCellSelected(r, c);
7809 }
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820 public boolean isAccessibleRowSelected(int r) {
7821 return JTable.this.isRowSelected(r);
7822 }
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833 public boolean isAccessibleColumnSelected(int c) {
7834 return JTable.this.isColumnSelected(c);
7835 }
7836
7837
7838
7839
7840
7841
7842
7843
7844 public int [] getSelectedAccessibleRows() {
7845 return JTable.this.getSelectedRows();
7846 }
7847
7848
7849
7850
7851
7852
7853
7854
7855 public int [] getSelectedAccessibleColumns() {
7856 return JTable.this.getSelectedColumns();
7857 }
7858
7859
7860
7861
7862
7863
7864
7865
7866 public int getAccessibleRowAtIndex(int i) {
7867 int columnCount = getAccessibleColumnCount();
7868 if (columnCount == 0) {
7869 return -1;
7870 } else {
7871 return (i / columnCount);
7872 }
7873 }
7874
7875
7876
7877
7878
7879
7880
7881
7882 public int getAccessibleColumnAtIndex(int i) {
7883 int columnCount = getAccessibleColumnCount();
7884 if (columnCount == 0) {
7885 return -1;
7886 } else {
7887 return (i % columnCount);
7888 }
7889 }
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899 public int getAccessibleIndexAt(int r, int c) {
7900 return ((r * getAccessibleColumnCount()) + c);
7901 }
7902
7903
7904
7905
7906
7907
7908
7909 protected class AccessibleJTableCell extends AccessibleContext
7910 implements Accessible, AccessibleComponent {
7911
7912 private JTable parent;
7913 private int row;
7914 private int column;
7915 private int index;
7916
7917
7918
7919
7920
7921 public AccessibleJTableCell(JTable t, int r, int c, int i) {
7922 parent = t;
7923 row = r;
7924 column = c;
7925 index = i;
7926 this.setAccessibleParent(parent);
7927 }
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937 public AccessibleContext getAccessibleContext() {
7938 return this;
7939 }
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949 protected AccessibleContext getCurrentAccessibleContext() {
7950 TableColumn aColumn = getColumnModel().getColumn(column);
7951 TableCellRenderer renderer = aColumn.getCellRenderer();
7952 if (renderer == null) {
7953 Class<?> columnClass = getColumnClass(column);
7954 renderer = getDefaultRenderer(columnClass);
7955 }
7956 Component component = renderer.getTableCellRendererComponent(
7957 JTable.this, getValueAt(row, column),
7958 false, false, row, column);
7959 if (component instanceof Accessible) {
7960 return component.getAccessibleContext();
7961 } else {
7962 return null;
7963 }
7964 }
7965
7966
7967
7968
7969
7970
7971
7972
7973 protected Component getCurrentComponent() {
7974 TableColumn aColumn = getColumnModel().getColumn(column);
7975 TableCellRenderer renderer = aColumn.getCellRenderer();
7976 if (renderer == null) {
7977 Class<?> columnClass = getColumnClass(column);
7978 renderer = getDefaultRenderer(columnClass);
7979 }
7980 return renderer.getTableCellRendererComponent(
7981 JTable.this, null, false, false,
7982 row, column);
7983 }
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993 public String getAccessibleName() {
7994 AccessibleContext ac = getCurrentAccessibleContext();
7995 if (ac != null) {
7996 String name = ac.getAccessibleName();
7997 if ((name != null) && (name != "")) {
7998
7999 return name;
8000 }
8001 }
8002 if ((accessibleName != null) && (accessibleName != "")) {
8003 return accessibleName;
8004 } else {
8005
8006 return (String)getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY);
8007 }
8008 }
8009
8010
8011
8012
8013
8014
8015 public void setAccessibleName(String s) {
8016 AccessibleContext ac = getCurrentAccessibleContext();
8017 if (ac != null) {
8018 ac.setAccessibleName(s);
8019 } else {
8020 super.setAccessibleName(s);
8021 }
8022 }
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034 public String getAccessibleDescription() {
8035 AccessibleContext ac = getCurrentAccessibleContext();
8036 if (ac != null) {
8037 return ac.getAccessibleDescription();
8038 } else {
8039 return super.getAccessibleDescription();
8040 }
8041 }
8042
8043
8044
8045
8046
8047
8048 public void setAccessibleDescription(String s) {
8049 AccessibleContext ac = getCurrentAccessibleContext();
8050 if (ac != null) {
8051 ac.setAccessibleDescription(s);
8052 } else {
8053 super.setAccessibleDescription(s);
8054 }
8055 }
8056
8057
8058
8059
8060
8061
8062
8063
8064 public AccessibleRole getAccessibleRole() {
8065 AccessibleContext ac = getCurrentAccessibleContext();
8066 if (ac != null) {
8067 return ac.getAccessibleRole();
8068 } else {
8069 return AccessibleRole.UNKNOWN;
8070 }
8071 }
8072
8073
8074
8075
8076
8077
8078
8079
8080 public AccessibleStateSet getAccessibleStateSet() {
8081 AccessibleContext ac = getCurrentAccessibleContext();
8082 AccessibleStateSet as = null;
8083
8084 if (ac != null) {
8085 as = ac.getAccessibleStateSet();
8086 }
8087 if (as == null) {
8088 as = new AccessibleStateSet();
8089 }
8090 Rectangle rjt = JTable.this.getVisibleRect();
8091 Rectangle rcell = JTable.this.getCellRect(row, column, false);
8092 if (rjt.intersects(rcell)) {
8093 as.add(AccessibleState.SHOWING);
8094 } else {
8095 if (as.contains(AccessibleState.SHOWING)) {
8096 as.remove(AccessibleState.SHOWING);
8097 }
8098 }
8099 if (parent.isCellSelected(row, column)) {
8100 as.add(AccessibleState.SELECTED);
8101 } else if (as.contains(AccessibleState.SELECTED)) {
8102 as.remove(AccessibleState.SELECTED);
8103 }
8104 if ((row == getSelectedRow()) && (column == getSelectedColumn())) {
8105 as.add(AccessibleState.ACTIVE);
8106 }
8107 as.add(AccessibleState.TRANSIENT);
8108 return as;
8109 }
8110
8111
8112
8113
8114
8115
8116
8117
8118 public Accessible getAccessibleParent() {
8119 return parent;
8120 }
8121
8122
8123
8124
8125
8126
8127
8128
8129 public int getAccessibleIndexInParent() {
8130 return index;
8131 }
8132
8133
8134
8135
8136
8137
8138 public int getAccessibleChildrenCount() {
8139 AccessibleContext ac = getCurrentAccessibleContext();
8140 if (ac != null) {
8141 return ac.getAccessibleChildrenCount();
8142 } else {
8143 return 0;
8144 }
8145 }
8146
8147
8148
8149
8150
8151
8152
8153
8154 public Accessible getAccessibleChild(int i) {
8155 AccessibleContext ac = getCurrentAccessibleContext();
8156 if (ac != null) {
8157 Accessible accessibleChild = ac.getAccessibleChild(i);
8158 ac.setAccessibleParent(this);
8159 return accessibleChild;
8160 } else {
8161 return null;
8162 }
8163 }
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179 public Locale getLocale() {
8180 AccessibleContext ac = getCurrentAccessibleContext();
8181 if (ac != null) {
8182 return ac.getLocale();
8183 } else {
8184 return null;
8185 }
8186 }
8187
8188
8189
8190
8191
8192
8193
8194
8195 public void addPropertyChangeListener(PropertyChangeListener l) {
8196 AccessibleContext ac = getCurrentAccessibleContext();
8197 if (ac != null) {
8198 ac.addPropertyChangeListener(l);
8199 } else {
8200 super.addPropertyChangeListener(l);
8201 }
8202 }
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212 public void removePropertyChangeListener(PropertyChangeListener l) {
8213 AccessibleContext ac = getCurrentAccessibleContext();
8214 if (ac != null) {
8215 ac.removePropertyChangeListener(l);
8216 } else {
8217 super.removePropertyChangeListener(l);
8218 }
8219 }
8220
8221
8222
8223
8224
8225
8226
8227 public AccessibleAction getAccessibleAction() {
8228 return getCurrentAccessibleContext().getAccessibleAction();
8229 }
8230
8231
8232
8233
8234
8235
8236
8237
8238 public AccessibleComponent getAccessibleComponent() {
8239 return this;
8240 }
8241
8242
8243
8244
8245
8246
8247
8248
8249 public AccessibleSelection getAccessibleSelection() {
8250 return getCurrentAccessibleContext().getAccessibleSelection();
8251 }
8252
8253
8254
8255
8256
8257
8258
8259 public AccessibleText getAccessibleText() {
8260 return getCurrentAccessibleContext().getAccessibleText();
8261 }
8262
8263
8264
8265
8266
8267
8268
8269 public AccessibleValue getAccessibleValue() {
8270 return getCurrentAccessibleContext().getAccessibleValue();
8271 }
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282 public Color getBackground() {
8283 AccessibleContext ac = getCurrentAccessibleContext();
8284 if (ac instanceof AccessibleComponent) {
8285 return ((AccessibleComponent) ac).getBackground();
8286 } else {
8287 Component c = getCurrentComponent();
8288 if (c != null) {
8289 return c.getBackground();
8290 } else {
8291 return null;
8292 }
8293 }
8294 }
8295
8296
8297
8298
8299
8300
8301 public void setBackground(Color c) {
8302 AccessibleContext ac = getCurrentAccessibleContext();
8303 if (ac instanceof AccessibleComponent) {
8304 ((AccessibleComponent) ac).setBackground(c);
8305 } else {
8306 Component cp = getCurrentComponent();
8307 if (cp != null) {
8308 cp.setBackground(c);
8309 }
8310 }
8311 }
8312
8313
8314
8315
8316
8317
8318
8319 public Color getForeground() {
8320 AccessibleContext ac = getCurrentAccessibleContext();
8321 if (ac instanceof AccessibleComponent) {
8322 return ((AccessibleComponent) ac).getForeground();
8323 } else {
8324 Component c = getCurrentComponent();
8325 if (c != null) {
8326 return c.getForeground();
8327 } else {
8328 return null;
8329 }
8330 }
8331 }
8332
8333
8334
8335
8336
8337
8338 public void setForeground(Color c) {
8339 AccessibleContext ac = getCurrentAccessibleContext();
8340 if (ac instanceof AccessibleComponent) {
8341 ((AccessibleComponent) ac).setForeground(c);
8342 } else {
8343 Component cp = getCurrentComponent();
8344 if (cp != null) {
8345 cp.setForeground(c);
8346 }
8347 }
8348 }
8349
8350
8351
8352
8353
8354
8355
8356 public Cursor getCursor() {
8357 AccessibleContext ac = getCurrentAccessibleContext();
8358 if (ac instanceof AccessibleComponent) {
8359 return ((AccessibleComponent) ac).getCursor();
8360 } else {
8361 Component c = getCurrentComponent();
8362 if (c != null) {
8363 return c.getCursor();
8364 } else {
8365 Accessible ap = getAccessibleParent();
8366 if (ap instanceof AccessibleComponent) {
8367 return ((AccessibleComponent) ap).getCursor();
8368 } else {
8369 return null;
8370 }
8371 }
8372 }
8373 }
8374
8375
8376
8377
8378
8379
8380 public void setCursor(Cursor c) {
8381 AccessibleContext ac = getCurrentAccessibleContext();
8382 if (ac instanceof AccessibleComponent) {
8383 ((AccessibleComponent) ac).setCursor(c);
8384 } else {
8385 Component cp = getCurrentComponent();
8386 if (cp != null) {
8387 cp.setCursor(c);
8388 }
8389 }
8390 }
8391
8392
8393
8394
8395
8396
8397
8398 public Font getFont() {
8399 AccessibleContext ac = getCurrentAccessibleContext();
8400 if (ac instanceof AccessibleComponent) {
8401 return ((AccessibleComponent) ac).getFont();
8402 } else {
8403 Component c = getCurrentComponent();
8404 if (c != null) {
8405 return c.getFont();
8406 } else {
8407 return null;
8408 }
8409 }
8410 }
8411
8412
8413
8414
8415
8416
8417 public void setFont(Font f) {
8418 AccessibleContext ac = getCurrentAccessibleContext();
8419 if (ac instanceof AccessibleComponent) {
8420 ((AccessibleComponent) ac).setFont(f);
8421 } else {
8422 Component c = getCurrentComponent();
8423 if (c != null) {
8424 c.setFont(f);
8425 }
8426 }
8427 }
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437 public FontMetrics getFontMetrics(Font f) {
8438 AccessibleContext ac = getCurrentAccessibleContext();
8439 if (ac instanceof AccessibleComponent) {
8440 return ((AccessibleComponent) ac).getFontMetrics(f);
8441 } else {
8442 Component c = getCurrentComponent();
8443 if (c != null) {
8444 return c.getFontMetrics(f);
8445 } else {
8446 return null;
8447 }
8448 }
8449 }
8450
8451
8452
8453
8454
8455
8456 public boolean isEnabled() {
8457 AccessibleContext ac = getCurrentAccessibleContext();
8458 if (ac instanceof AccessibleComponent) {
8459 return ((AccessibleComponent) ac).isEnabled();
8460 } else {
8461 Component c = getCurrentComponent();
8462 if (c != null) {
8463 return c.isEnabled();
8464 } else {
8465 return false;
8466 }
8467 }
8468 }
8469
8470
8471
8472
8473
8474
8475 public void setEnabled(boolean b) {
8476 AccessibleContext ac = getCurrentAccessibleContext();
8477 if (ac instanceof AccessibleComponent) {
8478 ((AccessibleComponent) ac).setEnabled(b);
8479 } else {
8480 Component c = getCurrentComponent();
8481 if (c != null) {
8482 c.setEnabled(b);
8483 }
8484 }
8485 }
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496 public boolean isVisible() {
8497 AccessibleContext ac = getCurrentAccessibleContext();
8498 if (ac instanceof AccessibleComponent) {
8499 return ((AccessibleComponent) ac).isVisible();
8500 } else {
8501 Component c = getCurrentComponent();
8502 if (c != null) {
8503 return c.isVisible();
8504 } else {
8505 return false;
8506 }
8507 }
8508 }
8509
8510
8511
8512
8513
8514
8515 public void setVisible(boolean b) {
8516 AccessibleContext ac = getCurrentAccessibleContext();
8517 if (ac instanceof AccessibleComponent) {
8518 ((AccessibleComponent) ac).setVisible(b);
8519 } else {
8520 Component c = getCurrentComponent();
8521 if (c != null) {
8522 c.setVisible(b);
8523 }
8524 }
8525 }
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536 public boolean isShowing() {
8537 AccessibleContext ac = getCurrentAccessibleContext();
8538 if (ac instanceof AccessibleComponent) {
8539 if (ac.getAccessibleParent() != null) {
8540 return ((AccessibleComponent) ac).isShowing();
8541 } else {
8542
8543
8544
8545 return isVisible();
8546 }
8547 } else {
8548 Component c = getCurrentComponent();
8549 if (c != null) {
8550 return c.isShowing();
8551 } else {
8552 return false;
8553 }
8554 }
8555 }
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568 public boolean contains(Point p) {
8569 AccessibleContext ac = getCurrentAccessibleContext();
8570 if (ac instanceof AccessibleComponent) {
8571 Rectangle r = ((AccessibleComponent) ac).getBounds();
8572 return r.contains(p);
8573 } else {
8574 Component c = getCurrentComponent();
8575 if (c != null) {
8576 Rectangle r = c.getBounds();
8577 return r.contains(p);
8578 } else {
8579 return getBounds().contains(p);
8580 }
8581 }
8582 }
8583
8584
8585
8586
8587
8588
8589
8590 public Point getLocationOnScreen() {
8591 if (parent != null) {
8592 Point parentLocation = parent.getLocationOnScreen();
8593 Point componentLocation = getLocation();
8594 componentLocation.translate(parentLocation.x, parentLocation.y);
8595 return componentLocation;
8596 } else {
8597 return null;
8598 }
8599 }
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611 public Point getLocation() {
8612 if (parent != null) {
8613 Rectangle r = parent.getCellRect(row, column, false);
8614 if (r != null) {
8615 return r.getLocation();
8616 }
8617 }
8618 return null;
8619 }
8620
8621
8622
8623
8624 public void setLocation(Point p) {
8625
8626
8627
8628 }
8629
8630 public Rectangle getBounds() {
8631 if (parent != null) {
8632 return parent.getCellRect(row, column, false);
8633 } else {
8634 return null;
8635 }
8636 }
8637
8638 public void setBounds(Rectangle r) {
8639 AccessibleContext ac = getCurrentAccessibleContext();
8640 if (ac instanceof AccessibleComponent) {
8641 ((AccessibleComponent) ac).setBounds(r);
8642 } else {
8643 Component c = getCurrentComponent();
8644 if (c != null) {
8645 c.setBounds(r);
8646 }
8647 }
8648 }
8649
8650 public Dimension getSize() {
8651 if (parent != null) {
8652 Rectangle r = parent.getCellRect(row, column, false);
8653 if (r != null) {
8654 return r.getSize();
8655 }
8656 }
8657 return null;
8658 }
8659
8660 public void setSize (Dimension d) {
8661 AccessibleContext ac = getCurrentAccessibleContext();
8662 if (ac instanceof AccessibleComponent) {
8663 ((AccessibleComponent) ac).setSize(d);
8664 } else {
8665 Component c = getCurrentComponent();
8666 if (c != null) {
8667 c.setSize(d);
8668 }
8669 }
8670 }
8671
8672 public Accessible getAccessibleAt(Point p) {
8673 AccessibleContext ac = getCurrentAccessibleContext();
8674 if (ac instanceof AccessibleComponent) {
8675 return ((AccessibleComponent) ac).getAccessibleAt(p);
8676 } else {
8677 return null;
8678 }
8679 }
8680
8681 public boolean isFocusTraversable() {
8682 AccessibleContext ac = getCurrentAccessibleContext();
8683 if (ac instanceof AccessibleComponent) {
8684 return ((AccessibleComponent) ac).isFocusTraversable();
8685 } else {
8686 Component c = getCurrentComponent();
8687 if (c != null) {
8688 return c.isFocusTraversable();
8689 } else {
8690 return false;
8691 }
8692 }
8693 }
8694
8695 public void requestFocus() {
8696 AccessibleContext ac = getCurrentAccessibleContext();
8697 if (ac instanceof AccessibleComponent) {
8698 ((AccessibleComponent) ac).requestFocus();
8699 } else {
8700 Component c = getCurrentComponent();
8701 if (c != null) {
8702 c.requestFocus();
8703 }
8704 }
8705 }
8706
8707 public void addFocusListener(FocusListener l) {
8708 AccessibleContext ac = getCurrentAccessibleContext();
8709 if (ac instanceof AccessibleComponent) {
8710 ((AccessibleComponent) ac).addFocusListener(l);
8711 } else {
8712 Component c = getCurrentComponent();
8713 if (c != null) {
8714 c.addFocusListener(l);
8715 }
8716 }
8717 }
8718
8719 public void removeFocusListener(FocusListener l) {
8720 AccessibleContext ac = getCurrentAccessibleContext();
8721 if (ac instanceof AccessibleComponent) {
8722 ((AccessibleComponent) ac).removeFocusListener(l);
8723 } else {
8724 Component c = getCurrentComponent();
8725 if (c != null) {
8726 c.removeFocusListener(l);
8727 }
8728 }
8729 }
8730
8731 }
8732
8733
8734
8735
8736
8737
8738 private class AccessibleJTableHeaderCell extends AccessibleContext
8739 implements Accessible, AccessibleComponent {
8740
8741 private int row;
8742 private int column;
8743 private JTableHeader parent;
8744 private Component rendererComponent;
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754 public AccessibleJTableHeaderCell(int row, int column,
8755 JTableHeader parent,
8756 Component rendererComponent) {
8757 this.row = row;
8758 this.column = column;
8759 this.parent = parent;
8760 this.rendererComponent = rendererComponent;
8761 this.setAccessibleParent(parent);
8762 }
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772 public AccessibleContext getAccessibleContext() {
8773 return this;
8774 }
8775
8776
8777
8778
8779
8780 private AccessibleContext getCurrentAccessibleContext() {
8781 return rendererComponent.getAccessibleContext();
8782 }
8783
8784
8785
8786
8787 private Component getCurrentComponent() {
8788 return rendererComponent;
8789 }
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799 public String getAccessibleName() {
8800 AccessibleContext ac = getCurrentAccessibleContext();
8801 if (ac != null) {
8802 String name = ac.getAccessibleName();
8803 if ((name != null) && (name != "")) {
8804 return ac.getAccessibleName();
8805 }
8806 }
8807 if ((accessibleName != null) && (accessibleName != "")) {
8808 return accessibleName;
8809 } else {
8810 return null;
8811 }
8812 }
8813
8814
8815
8816
8817
8818
8819 public void setAccessibleName(String s) {
8820 AccessibleContext ac = getCurrentAccessibleContext();
8821 if (ac != null) {
8822 ac.setAccessibleName(s);
8823 } else {
8824 super.setAccessibleName(s);
8825 }
8826 }
8827
8828
8829
8830
8831
8832
8833
8834
8835 public String getAccessibleDescription() {
8836 AccessibleContext ac = getCurrentAccessibleContext();
8837 if (ac != null) {
8838 return ac.getAccessibleDescription();
8839 } else {
8840 return super.getAccessibleDescription();
8841 }
8842 }
8843
8844
8845
8846
8847
8848
8849 public void setAccessibleDescription(String s) {
8850 AccessibleContext ac = getCurrentAccessibleContext();
8851 if (ac != null) {
8852 ac.setAccessibleDescription(s);
8853 } else {
8854 super.setAccessibleDescription(s);
8855 }
8856 }
8857
8858
8859
8860
8861
8862
8863
8864
8865 public AccessibleRole getAccessibleRole() {
8866 AccessibleContext ac = getCurrentAccessibleContext();
8867 if (ac != null) {
8868 return ac.getAccessibleRole();
8869 } else {
8870 return AccessibleRole.UNKNOWN;
8871 }
8872 }
8873
8874
8875
8876
8877
8878
8879
8880
8881 public AccessibleStateSet getAccessibleStateSet() {
8882 AccessibleContext ac = getCurrentAccessibleContext();
8883 AccessibleStateSet as = null;
8884
8885 if (ac != null) {
8886 as = ac.getAccessibleStateSet();
8887 }
8888 if (as == null) {
8889 as = new AccessibleStateSet();
8890 }
8891 Rectangle rjt = JTable.this.getVisibleRect();
8892 Rectangle rcell = JTable.this.getCellRect(row, column, false);
8893 if (rjt.intersects(rcell)) {
8894 as.add(AccessibleState.SHOWING);
8895 } else {
8896 if (as.contains(AccessibleState.SHOWING)) {
8897 as.remove(AccessibleState.SHOWING);
8898 }
8899 }
8900 if (JTable.this.isCellSelected(row, column)) {
8901 as.add(AccessibleState.SELECTED);
8902 } else if (as.contains(AccessibleState.SELECTED)) {
8903 as.remove(AccessibleState.SELECTED);
8904 }
8905 if ((row == getSelectedRow()) && (column == getSelectedColumn())) {
8906 as.add(AccessibleState.ACTIVE);
8907 }
8908 as.add(AccessibleState.TRANSIENT);
8909 return as;
8910 }
8911
8912
8913
8914
8915
8916
8917
8918
8919 public Accessible getAccessibleParent() {
8920 return parent;
8921 }
8922
8923
8924
8925
8926
8927
8928
8929
8930 public int getAccessibleIndexInParent() {
8931 return column;
8932 }
8933
8934
8935
8936
8937
8938
8939 public int getAccessibleChildrenCount() {
8940 AccessibleContext ac = getCurrentAccessibleContext();
8941 if (ac != null) {
8942 return ac.getAccessibleChildrenCount();
8943 } else {
8944 return 0;
8945 }
8946 }
8947
8948
8949
8950
8951
8952
8953
8954
8955 public Accessible getAccessibleChild(int i) {
8956 AccessibleContext ac = getCurrentAccessibleContext();
8957 if (ac != null) {
8958 Accessible accessibleChild = ac.getAccessibleChild(i);
8959 ac.setAccessibleParent(this);
8960 return accessibleChild;
8961 } else {
8962 return null;
8963 }
8964 }
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980 public Locale getLocale() {
8981 AccessibleContext ac = getCurrentAccessibleContext();
8982 if (ac != null) {
8983 return ac.getLocale();
8984 } else {
8985 return null;
8986 }
8987 }
8988
8989
8990
8991
8992
8993
8994
8995
8996 public void addPropertyChangeListener(PropertyChangeListener l) {
8997 AccessibleContext ac = getCurrentAccessibleContext();
8998 if (ac != null) {
8999 ac.addPropertyChangeListener(l);
9000 } else {
9001 super.addPropertyChangeListener(l);
9002 }
9003 }
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013 public void removePropertyChangeListener(PropertyChangeListener l) {
9014 AccessibleContext ac = getCurrentAccessibleContext();
9015 if (ac != null) {
9016 ac.removePropertyChangeListener(l);
9017 } else {
9018 super.removePropertyChangeListener(l);
9019 }
9020 }
9021
9022
9023
9024
9025
9026
9027
9028 public AccessibleAction getAccessibleAction() {
9029 return getCurrentAccessibleContext().getAccessibleAction();
9030 }
9031
9032
9033
9034
9035
9036
9037
9038
9039 public AccessibleComponent getAccessibleComponent() {
9040 return this;
9041 }
9042
9043
9044
9045
9046
9047
9048
9049
9050 public AccessibleSelection getAccessibleSelection() {
9051 return getCurrentAccessibleContext().getAccessibleSelection();
9052 }
9053
9054
9055
9056
9057
9058
9059
9060 public AccessibleText getAccessibleText() {
9061 return getCurrentAccessibleContext().getAccessibleText();
9062 }
9063
9064
9065
9066
9067
9068
9069
9070 public AccessibleValue getAccessibleValue() {
9071 return getCurrentAccessibleContext().getAccessibleValue();
9072 }
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083 public Color getBackground() {
9084 AccessibleContext ac = getCurrentAccessibleContext();
9085 if (ac instanceof AccessibleComponent) {
9086 return ((AccessibleComponent) ac).getBackground();
9087 } else {
9088 Component c = getCurrentComponent();
9089 if (c != null) {
9090 return c.getBackground();
9091 } else {
9092 return null;
9093 }
9094 }
9095 }
9096
9097
9098
9099
9100
9101
9102 public void setBackground(Color c) {
9103 AccessibleContext ac = getCurrentAccessibleContext();
9104 if (ac instanceof AccessibleComponent) {
9105 ((AccessibleComponent) ac).setBackground(c);
9106 } else {
9107 Component cp = getCurrentComponent();
9108 if (cp != null) {
9109 cp.setBackground(c);
9110 }
9111 }
9112 }
9113
9114
9115
9116
9117
9118
9119
9120 public Color getForeground() {
9121 AccessibleContext ac = getCurrentAccessibleContext();
9122 if (ac instanceof AccessibleComponent) {
9123 return ((AccessibleComponent) ac).getForeground();
9124 } else {
9125 Component c = getCurrentComponent();
9126 if (c != null) {
9127 return c.getForeground();
9128 } else {
9129 return null;
9130 }
9131 }
9132 }
9133
9134
9135
9136
9137
9138
9139 public void setForeground(Color c) {
9140 AccessibleContext ac = getCurrentAccessibleContext();
9141 if (ac instanceof AccessibleComponent) {
9142 ((AccessibleComponent) ac).setForeground(c);
9143 } else {
9144 Component cp = getCurrentComponent();
9145 if (cp != null) {
9146 cp.setForeground(c);
9147 }
9148 }
9149 }
9150
9151
9152
9153
9154
9155
9156
9157 public Cursor getCursor() {
9158 AccessibleContext ac = getCurrentAccessibleContext();
9159 if (ac instanceof AccessibleComponent) {
9160 return ((AccessibleComponent) ac).getCursor();
9161 } else {
9162 Component c = getCurrentComponent();
9163 if (c != null) {
9164 return c.getCursor();
9165 } else {
9166 Accessible ap = getAccessibleParent();
9167 if (ap instanceof AccessibleComponent) {
9168 return ((AccessibleComponent) ap).getCursor();
9169 } else {
9170 return null;
9171 }
9172 }
9173 }
9174 }
9175
9176
9177
9178
9179
9180
9181 public void setCursor(Cursor c) {
9182 AccessibleContext ac = getCurrentAccessibleContext();
9183 if (ac instanceof AccessibleComponent) {
9184 ((AccessibleComponent) ac).setCursor(c);
9185 } else {
9186 Component cp = getCurrentComponent();
9187 if (cp != null) {
9188 cp.setCursor(c);
9189 }
9190 }
9191 }
9192
9193
9194
9195
9196
9197
9198
9199 public Font getFont() {
9200 AccessibleContext ac = getCurrentAccessibleContext();
9201 if (ac instanceof AccessibleComponent) {
9202 return ((AccessibleComponent) ac).getFont();
9203 } else {
9204 Component c = getCurrentComponent();
9205 if (c != null) {
9206 return c.getFont();
9207 } else {
9208 return null;
9209 }
9210 }
9211 }
9212
9213
9214
9215
9216
9217
9218 public void setFont(Font f) {
9219 AccessibleContext ac = getCurrentAccessibleContext();
9220 if (ac instanceof AccessibleComponent) {
9221 ((AccessibleComponent) ac).setFont(f);
9222 } else {
9223 Component c = getCurrentComponent();
9224 if (c != null) {
9225 c.setFont(f);
9226 }
9227 }
9228 }
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238 public FontMetrics getFontMetrics(Font f) {
9239 AccessibleContext ac = getCurrentAccessibleContext();
9240 if (ac instanceof AccessibleComponent) {
9241 return ((AccessibleComponent) ac).getFontMetrics(f);
9242 } else {
9243 Component c = getCurrentComponent();
9244 if (c != null) {
9245 return c.getFontMetrics(f);
9246 } else {
9247 return null;
9248 }
9249 }
9250 }
9251
9252
9253
9254
9255
9256
9257 public boolean isEnabled() {
9258 AccessibleContext ac = getCurrentAccessibleContext();
9259 if (ac instanceof AccessibleComponent) {
9260 return ((AccessibleComponent) ac).isEnabled();
9261 } else {
9262 Component c = getCurrentComponent();
9263 if (c != null) {
9264 return c.isEnabled();
9265 } else {
9266 return false;
9267 }
9268 }
9269 }
9270
9271
9272
9273
9274
9275
9276 public void setEnabled(boolean b) {
9277 AccessibleContext ac = getCurrentAccessibleContext();
9278 if (ac instanceof AccessibleComponent) {
9279 ((AccessibleComponent) ac).setEnabled(b);
9280 } else {
9281 Component c = getCurrentComponent();
9282 if (c != null) {
9283 c.setEnabled(b);
9284 }
9285 }
9286 }
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297 public boolean isVisible() {
9298 AccessibleContext ac = getCurrentAccessibleContext();
9299 if (ac instanceof AccessibleComponent) {
9300 return ((AccessibleComponent) ac).isVisible();
9301 } else {
9302 Component c = getCurrentComponent();
9303 if (c != null) {
9304 return c.isVisible();
9305 } else {
9306 return false;
9307 }
9308 }
9309 }
9310
9311
9312
9313
9314
9315
9316 public void setVisible(boolean b) {
9317 AccessibleContext ac = getCurrentAccessibleContext();
9318 if (ac instanceof AccessibleComponent) {
9319 ((AccessibleComponent) ac).setVisible(b);
9320 } else {
9321 Component c = getCurrentComponent();
9322 if (c != null) {
9323 c.setVisible(b);
9324 }
9325 }
9326 }
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337 public boolean isShowing() {
9338 AccessibleContext ac = getCurrentAccessibleContext();
9339 if (ac instanceof AccessibleComponent) {
9340 if (ac.getAccessibleParent() != null) {
9341 return ((AccessibleComponent) ac).isShowing();
9342 } else {
9343
9344
9345
9346 return isVisible();
9347 }
9348 } else {
9349 Component c = getCurrentComponent();
9350 if (c != null) {
9351 return c.isShowing();
9352 } else {
9353 return false;
9354 }
9355 }
9356 }
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369 public boolean contains(Point p) {
9370 AccessibleContext ac = getCurrentAccessibleContext();
9371 if (ac instanceof AccessibleComponent) {
9372 Rectangle r = ((AccessibleComponent) ac).getBounds();
9373 return r.contains(p);
9374 } else {
9375 Component c = getCurrentComponent();
9376 if (c != null) {
9377 Rectangle r = c.getBounds();
9378 return r.contains(p);
9379 } else {
9380 return getBounds().contains(p);
9381 }
9382 }
9383 }
9384
9385
9386
9387
9388
9389
9390
9391 public Point getLocationOnScreen() {
9392 if (parent != null) {
9393 Point parentLocation = parent.getLocationOnScreen();
9394 Point componentLocation = getLocation();
9395 componentLocation.translate(parentLocation.x, parentLocation.y);
9396 return componentLocation;
9397 } else {
9398 return null;
9399 }
9400 }
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412 public Point getLocation() {
9413 if (parent != null) {
9414 Rectangle r = parent.getHeaderRect(column);
9415 if (r != null) {
9416 return r.getLocation();
9417 }
9418 }
9419 return null;
9420 }
9421
9422
9423
9424
9425
9426
9427 public void setLocation(Point p) {
9428 }
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439 public Rectangle getBounds() {
9440 if (parent != null) {
9441 return parent.getHeaderRect(column);
9442 } else {
9443 return null;
9444 }
9445 }
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455 public void setBounds(Rectangle r) {
9456 AccessibleContext ac = getCurrentAccessibleContext();
9457 if (ac instanceof AccessibleComponent) {
9458 ((AccessibleComponent) ac).setBounds(r);
9459 } else {
9460 Component c = getCurrentComponent();
9461 if (c != null) {
9462 c.setBounds(r);
9463 }
9464 }
9465 }
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477 public Dimension getSize() {
9478 if (parent != null) {
9479 Rectangle r = parent.getHeaderRect(column);
9480 if (r != null) {
9481 return r.getSize();
9482 }
9483 }
9484 return null;
9485 }
9486
9487
9488
9489
9490
9491
9492
9493 public void setSize (Dimension d) {
9494 AccessibleContext ac = getCurrentAccessibleContext();
9495 if (ac instanceof AccessibleComponent) {
9496 ((AccessibleComponent) ac).setSize(d);
9497 } else {
9498 Component c = getCurrentComponent();
9499 if (c != null) {
9500 c.setSize(d);
9501 }
9502 }
9503 }
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513 public Accessible getAccessibleAt(Point p) {
9514 AccessibleContext ac = getCurrentAccessibleContext();
9515 if (ac instanceof AccessibleComponent) {
9516 return ((AccessibleComponent) ac).getAccessibleAt(p);
9517 } else {
9518 return null;
9519 }
9520 }
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533 public boolean isFocusTraversable() {
9534 AccessibleContext ac = getCurrentAccessibleContext();
9535 if (ac instanceof AccessibleComponent) {
9536 return ((AccessibleComponent) ac).isFocusTraversable();
9537 } else {
9538 Component c = getCurrentComponent();
9539 if (c != null) {
9540 return c.isFocusTraversable();
9541 } else {
9542 return false;
9543 }
9544 }
9545 }
9546
9547
9548
9549
9550
9551
9552
9553 public void requestFocus() {
9554 AccessibleContext ac = getCurrentAccessibleContext();
9555 if (ac instanceof AccessibleComponent) {
9556 ((AccessibleComponent) ac).requestFocus();
9557 } else {
9558 Component c = getCurrentComponent();
9559 if (c != null) {
9560 c.requestFocus();
9561 }
9562 }
9563 }
9564
9565
9566
9567
9568
9569
9570
9571
9572 public void addFocusListener(FocusListener l) {
9573 AccessibleContext ac = getCurrentAccessibleContext();
9574 if (ac instanceof AccessibleComponent) {
9575 ((AccessibleComponent) ac).addFocusListener(l);
9576 } else {
9577 Component c = getCurrentComponent();
9578 if (c != null) {
9579 c.addFocusListener(l);
9580 }
9581 }
9582 }
9583
9584
9585
9586
9587
9588
9589
9590
9591 public void removeFocusListener(FocusListener l) {
9592 AccessibleContext ac = getCurrentAccessibleContext();
9593 if (ac instanceof AccessibleComponent) {
9594 ((AccessibleComponent) ac).removeFocusListener(l);
9595 } else {
9596 Component c = getCurrentComponent();
9597 if (c != null) {
9598 c.removeFocusListener(l);
9599 }
9600 }
9601 }
9602
9603 }
9604
9605 }
9606
9607 }